_javascript Techniques for parameter usage analysis in Javascript functions

Source: Internet
Author: User
First, in JS, a function is also an identifier that can be assigned to a new variable, or it can be called by this variable. This is a bit like the C language function pointer, but it's not exactly the same as the following code:
Copy Code code as follows:

function Myfun () {
Alert ("Funcation call");
}
var fun = myfun;
Fun ();

Another thing worth saying is that the parameters of the function in JS is not necessarily a strict match, the usual programming experience, such as a function fun (AA,BB), then we call this function should be passed to him two parameters. But in JS, we can give him any parameters, 1, 3, etc., can be. The parameter passes in JS, not exactly the arguments that are specified when the function is declared, each time a function is called, there is an array named arguments, which stores all the arguments passed in when the function is called, and with it we can even stop specifying formal arguments when the function is declared. The following code:
Copy Code code as follows:

function args () {
if (Arguments[0]!= undefined) {
Alert (arguments[0]);
}
}
Args (); Do not output anything
Args ("hehe"); Eject parameter value

As shown above, each subscript of the arguments array, starting at 0, corresponds to each parameter passed in, and if there is no argument at the specified position, then he will be undefined,
We can use Arguments.length to determine the number of arguments passed in, which is sometimes useful, for example, we can use this feature to simulate the C language's printf function:
Copy Code code 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 code above, simple to achieve the basic function of the formatted output, of course, if you are interested, you can do it better, finally say another bar, arguments also has a callee attribute, which represents the currently tuned function, this attribute value in some cases is useful. Consider the following code:
Copy Code code 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 of the function, mysum first and sum is the same function, if the program in the execution process of the SUM function is changed, then call the mysum result will be different, if the function to this, there will not be such a problem ~
Copy Code code 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 above, no matter how the outside reference to change callee will point to the current function, in the use of JS write recursion, this is still necessary to pay attention to, although generally not
This error can occur, but if it is not easy to find the reason, it will waste a lot of time.

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.