js带参数方法有哪些?

JavaScript011

js带参数方法有哪些?,第1张

JS调用后台带参数,参考方法如下:\x0d\x0a后台方法:\x0d\x0aprotected string CsharpVoid(string strCC)\x0d\x0a{\x0d\x0a strCC = "你好!" + strCC\x0d\x0a return strCC\x0d\x0a}\x0d\x0a\x0d\x0a前台JS如下:\x0d\x0afunction Init()\x0d\x0a{\x0d\x0a var v = "中国"\x0d\x0a var s = ''\x0d\x0a alert(s)\x0d\x0a }

JS调用后台带参数,参考方法如下:

后台方法:

protected string CsharpVoid(string strCC)

{

strCC = "你好!" + strCC

return strCC

}

前台JS如下:

function Init()

{

var v = "中国"

var s = '<%=CsharpVoid("'+v+'") %>'

alert(s)

}

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)的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。