Home >Web Front-end >JS Tutorial >Analysis of parameter usage in Javascript functions_javascript skills

Analysis of parameter usage in Javascript functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:31:31808browse

First of all, a function is also an identifier in JS. It can be assigned to a new variable or called through this variable. This is a bit like a function pointer in C language, but it is not exactly the same, such as the following code:

Copy code The code is as follows:

function myfun() {
alert("funcation call");
}
var fun = myfun;
fun();

Another thing worth mentioning is that the parameters of functions in JS do not necessarily match strictly. Common programming experience, for example, if there is such a function fun(aa,bb), then when we call this function Two parameters should be passed to him. But in JS, we can pass any number of parameters to him, 1, 3, etc., it’s all fine. Parameter passing in JS is not exactly based on the parameters specified when the function is declared. Every time a function is called, there will be an array named arguments. This array stores all the parameters passed in when the function is called. With it, we can even specify formal parameters when declaring the function, as in the following code:
Copy code The code is as follows:

function args() {
if (arguments[0] != undefined) {
alert(arguments[0]);
}
}
args( ); //Output nothing
args("hehe"); //Pop up parameter values ​​

As shown above, each subscript of the arguments array starts from 0 and corresponds to For each parameter passed in, if there is no parameter at the specified position, then it will be undefined.
We can use arguments.length to determine the number of parameters passed in. This method is sometimes very useful. For example, we You can use this feature to simulate the printf function of C language:
Copy code The code is as follows:

function format() {
if (arguments.length == 0) {
return "";
}
var formatter = arguments[0];
for (var i = 1; i < arguments.length; i ) {
formatter = formatter.replace(new RegExp("\{" (i-1) "\}","gm"), arguments[i]);
}
return formatter;
}
alert(format("Hello {0},this is the fetures of {1}!","world","javascript"));

The above code simply implements the basic function of formatted output. Of course, if you are interested, you can make it better. Let me talk about one last thing. arguments also has a callee attribute, which represents the current Function that can be adjusted. This attribute value is still useful in some cases. Consider the following code:
Copy code The code is as follows:

function sum(num) {
if (num == 1) {
return num;
} else {
return num sum(num - 1);
}
}
var mysum = sum;
alert(mysum(5)); //Output 15
sum = function() { return 1; };
alert(mysum(5)); //Output 6

This is a recursive summation function. Mysum is the same function as sum at the beginning. If the sum function body is changed during program execution, the result of calling mysum will be different. If the function is changed to this, There will be no such problem~
Copy code The code is as follows:

function sum( num) {
if (num == 1) {
return num;
} else {
return num arguments.callee(num - 1);
}
}
var mysum = sum;
alert(mysum(5)); //output 15
sum = function() { return 1; };
alert(mysum(5)); //output 15

As shown above, no matter how the external reference changes, callee will point to the current called function. When writing recursion in JS, it is still necessary to pay attention to this, although it is generally not
This kind of error will occur, but if it occurs, it is not easy to find the cause, and a lot of time will be wasted.
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