Home >Web Front-end >JS Tutorial >Introduction to the use of two common methods of function calling in js_javascript skills
A js function
function test(aa){ window.alert("你输入的是"+aa); }
Method 1: Call
directlytest("dddd");
Method 2: Assign function to variable
var abc=test;
abc('China');//Use variables to call functions
Note:
When we write it in this form, var abc=test("dddd"); cannot call the function through the variable abc.
This way of writing will assign the return value to abc when test has a return value. When there is no return value, the value of abc is undefined.
Specially emphasize that js naturally supports variable parameters
//编写一个函数,可以接受任意多个数,并计算他们的和 //无法重载,abc2函数名不能重复 function abc2(n1){// 可以无视参数n1 //在js中有一个arguments,可以访问所有传入的值。 //window.alert(arguments.length); //遍历所有的参数 for(var i=0;i<arguments.length;i++){ window.alert(arguments[i]); } }