Home  >  Article  >  Web Front-end  >  js中函数调用的两种常用方法使用介绍_javascript技巧

js中函数调用的两种常用方法使用介绍_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:41:461206browse

一个js函数

function test(aa){
window.alert("你输入的是"+aa);
}

方法一:直接调用

test("dddd");

方法二:函数赋值给变量

var abc=test;

abc('中国');//用变量来调用函数

注意:

当我们写成这种形式的时候,var abc=test("dddd"); 不能通过变量abc来调用函数。

这种写法当test有返回值的时候会把返回值赋值给abc,当没有返回值的时候abc的值为undefine。

特别强调js天然就支持可变参数

//编写一个函数,可以接受任意多个数,并计算他们的和
//无法重载,abc2函数名不能重复
function abc2(n1){// 可以无视参数n1
//在js中有一个arguments,可以访问所有传入的值。
//window.alert(arguments.length);
//遍历所有的参数
for(var i=0;i
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn