Before referring to the above concept, first want to talk about the hidden parameters of the function in the function in JavaScript: Arguments
Arguments
This object represents the parameters of the function that is executed and calls 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.
illustrate
Arguments are a hidden object created in addition to the specified parameters when the function calls. 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 an objects 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 += "/n/n"
For (i = 0; I <numargs; I ++) {// Get the parameter content.
s + = "arg" + i + "=" + arguments [i] + "/n";
}
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.
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 the compile text 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
The optional function parameter is the name of the Function object currently being executed.
illustrate
The initial value of the Callee attribute is the Function object that is 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 0;
else
Return n + arguments.callee (n -1)
}
More general recursion functions:
Copy code code as follows:
var sum = function (n) {
if (n <= 0)
Return 0;
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 to say, all the this This pointer inside the function will be assigned to thisarg, which can achieve the purpose of running the function as another object.
Apply's description
If Argarray is not an effective array or is not an Arguments object, it will cause a Typerror.
Without any parameters of Argarray and THISARG, the Global object will be used as thisArg,
And cannot be passed on any parameters.
Call's description
The call method can change the context of an object of a function from the initial context to a new object specified by thisarg.
If not provided thisarg parameters, then the GLOBAL object is used as thisarg
Related skills:
There is another technique in the application of call and Apply, that is, after using another function (class) with call and Apply, the current function (class) has another method (class) method or attribute. This can also be called It is "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 defined model in the JavaScript framework propro.
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, you can implement the class creation mode in Prototype
Example:
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 vene ("moto");
moto.showself ();