js中函数的参数为对象时候怎么操作

JavaScript010

js中函数的参数为对象时候怎么操作,第1张

直接传函数名 比如funcB 在函数里面 直接执行funcB()

var b = function(){alert(1)}

var a = function(v1,v2){v2()}

a(1,b)

JS函数的参数在function内可以用arguments对象来获取。

参数的调用有两种方式:

1、期望参数的使用。

2、实际传递参数的使用。

应用举例:

function

Test(a,

b){

var

i,

s

=

"Test函数有"

var

numargs

=

arguments.length

//

获取实际被传递参数的数值。

var

expargs

=

Test.length

//

获取期望参数的数值,函数定义时的预期参数个数(有a和b

2个参数)。

s

+=

(expargs

+

"个参数。")

s

+=

"\n\n"

for

(i

=0

i

<

numargs

i++){

//

获取参数内容。

s

+=

"

第"

+

i

+

"个参数是:"

+

arguments[i]

+

"\n"

}

return(s)

//

返回参数列表。

}

alert(Test('param1','second

param','第三个参数'))

需要注意的是:

arguments是一个object对象,它不是数组,不能对它使用shift、push、join等方法。

上述举例时用的arguments[i]中的i只是作为arguments对象的属性,并不能理解为数组下标。

代码演示

<html>

<head>

<script

language="javascript">

function

reloadList(){

if(typeof

arguments[0]

==

"function"){

arguments[0].call(this)

arguments[0]()

}

if(typeof

arguments[0]

==

"string")

alert(arguments[0])

if(typeof

arguments[0]

==

"number")

alert(arguments[0])

if(typeof

arguments[0]

==

"undefined")

alert(arguments[0])

if(typeof

arguments[0]

==

"boolean")

alert(arguments[0])

if(typeof

arguments[0]

==

"null")

alert(arguments[0])

}

reloadList(function(){})

</script>

</head>

<body>

</body>

以上这篇深入理解JS函数的参数(arguments)的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

这不是很方便吗?这样就可以直接被函数做参数传到需要回调的其他函数中使用,java中的接口调用需要实例化,js就不需要了呀。

函数自定义方法,其实可以把你说的函数看做java中的类。比如:

function Example(name){this.name=name}

Example.prototype.setName=function(name){

this.name=name

return this

}

Example.prototype.getName=function(){

return this.name

}

new Example('name').getName()//name

new Example('name').setName('name1').getName()//name1

无论是js中的Array,String……都有类似的prototype类型描述对象,当这些类型实例化以后可直接调用prototype中定义的方法或属性,也可覆盖。在实例化中prototype会被简单化成__proto__对象,可以通过console.log进行观察。关于继承,所有的js对象都有一个顶级的被继承对象,Object,方法有很多,通常被提到的就是定义类型函数时给prototype赋个你想继承的类型的实例化object,但这种方法很蠢,自己看书,搜索,实践吧,手机打字太累了。