Analysis of parameter usage in Javascript Functions

Source: Internet
Author: User

First, the 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 the function pointer in C language, but it is not exactly the same, the following code:
Copy codeThe 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, such as having such a function fun (aa, bb ), when calling this function, we should pass two parameters to it. However, in JS, we can pass any parameter, one parameter, three parameters, and so on. Parameters passed in JS are not exactly the parameters specified during function declaration. Each time a function is called, an array named arguments is displayed, this array stores all the parameters passed in when a function is called. With this array, we can even stop specifying formal parameters when the function is declared. The code below is as follows:
Copy codeThe Code is as follows:
Function args (){
If (arguments [0]! = Undefined ){
Alert (arguments [0]);
}
}
Args (); // No output
Args ("hehe"); // The parameter value is displayed.

As shown above, each subscript of the arguments array corresponds to each parameter passed in from 0. If no parameter is specified, it will be undefined,
We can use arguments. length to determine the number of passed parameters. This method is sometimes useful. For example, we can use this feature to simulate the printf function in C language:
Copy codeThe 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 implements the basic function of formatting output. Of course, if you are interested, you can make it better. Let's talk about it. arguments also has a callee attribute, it represents the currently called function. This attribute value is useful in some cases. Consider the following code:
Copy codeThe 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 as sum at the beginning. If the sum function is changed during program execution, the results of calling mysum will be different, if you change the function to this type, no such problem will occur ~
Copy codeThe 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 callee is changed by reference outside, it will point to the current called function. When writing recursion using JS, it is worth noting that although it is generally not
This error may occur, but it is not easy to find the cause, and it will waste a lot of time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.