Before referring to the above concept, first want to talk about the hidden parameters of the function in the function in JavaScript:
Arguments
Arguments represent the parameters that are executed and the function of calling its function. [function.] arguments [n] parameter
Function: Options. The name of the Function object is currently executed.
n: Options. To pass the parameter value index from 0 to the Function object. It shows that Argumeents is a hidden object created in addition to the specified parameter when the function is called. Arguments are an object similar to an array but not an array. It is said that it has an array because it has an array -like access nature and method. It can access the value of the corresponding single parameter by arguments [n], and has the array length attribute length. There is also the parameter that the Arguments object stores the function that is actually passed to the function, not limited to the parameter list defined by the functional statement, and cannot be explicitly created to create an arguments object. The Arguments object can only be available at the beginning of the function.
The following example explains these properties in detail
Copy code code as follows:
// The usage of the arguments object
Function ARGTEST (a, B) {{
var I, s = "The ARGTEST FUNCTION Expected";
var numargs = arguments.Length; // Get the value of the passing parameters.
Var Expargs = Argtest.Length; // Get the value of the expected parameters.
if (expargs <2)
s + = expargs + "argument.";
else
s + = expargs + "arguments.";;
if (Numargs <2)
s + = numargs + "was passed.";
else
s + = numargs + "we passed.";
s += ""
For (i = 0; I <numargs; I ++) {// Get the parameter content.
s + = "Arg" + I + "=" + ARGUMENTS + "" "";
}
Return (s); // Return the parameter list.
}
Here is a code that explains that Arguments is not an array (Array class):
Copy code code as follows:
Array.prototype.setValue = 1;
alert (new array (). SelfValue);
function tests () {
Alert (ARGUMENTS.SELFVALUE);
}
You will find the first Alert display 1, which means that the array object has the SelfValue attribute with a value of 1. When you call the function testagumens, you will find that "Undefined" shows that it is not the attribute of Arguments, that is, the attribute of Arguments, that is, Arguments are not an array object.
Add a simple way to add here: Alert (ARGUMENTS Instanceof Array);
Alert (ARGUMENTS Instanceof Object);
Caller
Returns a reference to the function that calls the current function.
FunctionName.Caller
The FunctionName object is the name of the execution function.
illustrate
For functions, the Caller attribute is defined only when the function is executed. If the function is called from the top layer, then Caller contains NULL. If the Caller attribute is used in the context of the string, the result is the same as FunctionName.tstring, that is, it shows that the function of the function is displayed.
Note: Function.tostring () can realize the anti -compilation function of the function.
The following example illustrates the use of the Caller attribute:
Copy code code as follows:
// Caller Demo {
function calldemo () {
if (CallerDemo.caller) {
var a = CallerDemo.caller.tostring ();
alert (a);
} Else {
Alert ("this is a top function");
}
}
function handlecaller () {
CallerDemo ();
}
Callee
Returns the text of the Function object that is being executed, the text of the specified Function object. [Function.] Arguments.Callee optional parameter is the name of the Function object currently being executed. Explain that the initial value of the Callee attribute is the Function object that is being executed. The Callee attribute is a member of the Arguments object, which indicates the reference to the function object itself, which is conducive to the recursiveness of the anonymous function or the packaging of the function. This attribute is only available when the correlation function is executed. It should be noted that Callee has a length attribute. This attribute is sometimes better used to verify. Arguments.Length is a solid parameter length, and arguments.Callee.Length is the length of the parameter, which can determine whether the length of the parameter is consistent with the actual parameter length.
Exemplary example
Copy code code as follows:
// Callee can print itself
function calledemo () {
Alert (ARGUMENTS.CALLEE);
}
// Used to verify parameters
Function CalleELELENGTHDEMO (ARG1, ARG2) {{
if (Arguments.Length == Arguments.callee.length) {
Window.alert ("Verifying the ginseng and solid parameter length is correct!");
Return;
} Else {
Alert ("solid parameter length:" +arguments.length);
Alert ("" Shape Permiated: +Arguments.Callee.length);
}
}
// Recursive calculation
var sum = function (n) {
if (n <= 0)
Return 1;
else
Return n + arguments.callee (n -1)
} Compared with general recursive functions: var Sum = Function (n) {
if (1 == n) Return 1;
Else Return n + Sum (n-1);
}
At the time of calling: Alert (Sum (100));
Among them, the function contains the reference to the SUM itself. The function name is just a variable name. Calling SUM inside the function is equivalent to calling a global variable. Good way.
apply and call
Their role is to bind the function to another object. The two only specifically differently define the parameter method:
apply (thisarg, argarray);
call (thisarg [, arg1, arg2 ...]);
That is, the this This pointer inside all functions will be assigned to thisArg, which can realize the purpose of the purpose of using the function as another method method.
If any parameter of Argarray and THISARG is not provided, the Global object will be used as thisArg and cannot be passed on any parameters. Call's description Call method can change the context of an object of a function from the initial context to a new object specified by Thisarg.
If you do not provide a ThisArg parameter, then the Global object is used as a THISARG -related technique: After applying Call and Apply, there is another technique in it. After using another function (class), the current function (classes) has the current function (class). Another function (class) method or attribute, which can also be called "inheritance".
Look at the following example:
Copy code code as follows:
// Inherited demonstration
Function base () {
this.member = "dnnnsun_member";
this.method = function () {
Window.alert (this.member);
}
}
function expedition () {) {
base.call (this);
Window.alert (Member);
Window.alert (this.Method);
}
The above examples can be seen that after Call, Extend can inherit the method and attributes of Base. By the way, use Apply to create a definition model in the JavaScript framework prototype. In fact, the current code is as follows:
Copy code code as follows:
var class = {
Create: FUNCTION () {
Return function () {
this.initialize.apply (this, arguments);
}
}
}
Analysis: From the code, the object contains only one method: create, which returns a function, that is, class. But this is also a constructor of the class, where the initialize is called, and this method is the initialization function defined during the creation of the class. Through this way, the class creation pattern examples in Prototype can be implemented:
Copy code code as follows:
var Vehicle = class.create ();
vehicle.prototype = {
initialize: function (type) {
this.type = type;
}
ShowSelf: Function () {{)
Alert ("this very is"+ this.type);
}
} Var Moto = New Vehicle ("Moto");
moto.showself ();