1. Four ways to call a function
Third Type: constructor callif the constructor call contains a set of argument lists inside the parentheses, the argument expressions are evaluated first and then passed inside the function. This is consistent with the function call and the method invocation. However, if the constructor does not have formal parameters, thesyntax of the JavaScript constructor call is to allow omitting the argument list and the parentheses. such as:
var o=New Object (); // is equivalent to var o=new Object;
Fourth: Use Call () and apply () indirect calls (in detail later)
2. Real-participation parameters for functions--optional parameterslet's look at an example:
function geta (o,/*optional* / a ) {if(a=== Undefined) { a=[]; } for (var in o) { a.push (pro); }}
the above function call can pass in a parameter, or two parameters.
A: It is common to add/*optional* to the front of an optional parameter to emphasize that the parameter is optional.
B: There is also a general option to place the optional parameters at the end of the parameter list.
C: If an optional parameter does not pass in an argument, it will default to undefined.
D: Of course, you can also participate in null or undefined as a placeholder.
3. Variable-length argument list: Argument objectSometimes when we call a function and pass a parameter that exceeds the number of function definitions, there is no way to directly get an unnamed worthy reference. Althoughthe default behavior of J Avascript will automatically omit the extra parameters . But sometimes we just need to get these extra parameters. (Isn't it a bit cheap?) --)The Iron Man is here! Amount No! Is the argument object to solve the problem. in the body of a function, arguments is a reference to an argument object. An argument object is a class array object. This can be accessed by subscript, without having to be accessed by name. duang~Here's an example:
function A (x, y ) {return x*y;} // call A (' 10 ', ' 10 ', ' 11 ');
11 Default ellipsis, if we want to get it, we can change the above example:
function A (x, y ) {if(arguments.length!=2) { return x*y; } Else if (arguments.length>2) { return arguments[2]; // the arguments[2 here] is 11. } }
An important use of an argument object is to allow the function to manipulate the arguments of the number of tasks. For example:
function Max (/*... */ ) { var max=number.negative_infinity; for (var i=0;i<arguments.length;i++) { if(arguments[i]>max) { Max=arguments[i]; } } return Max;} var l=max (N/a); alert (L)
in 4.JavaScript, a function is not only used as a syntax, but can be defined to be called. is also a value. let's look at a function definition:
function squ (x) { return x*x;}
Understanding: This definition creates a new function object and assigns it to the variable squ. The name of the function is actually invisible, it (squ) is simply the name of the variable, and this side variable refers to the function object. functions can also be assigned to other variables, and still work correctly.
var s=squ; // here S and squ refer to the same function squ (4); // + S (4); // -
You can also assign a function to an object's properties.
var o={ squ:function(x) { return x*x; }} var y=o.squ (4); // -
JavaScript Authority design--javascript function (Brief study note 11)