JavaScript is holding the cheat note function call passed two implicit parameters Argument,this
Implicit: means that these parameters do not display columns in the function signature, but they are silently passed to the function in the scope of the function.
Inside functions They can be used like cards to display named arguments
If you're debugging in your browser, you're going to happen they're real. arguments parameters in objects
He's just an array of classes, just having some properties, you can get the array form to get the first argument (arguments[0), or for loop traversal, but he's not the array this argument
This is called the context of the caller
And the function calls several ways. Called as a function, is the simplest form. Called as a method, invoked on an object, and supports object-oriented programming. Called as a constructor, creating a new object. Calling through the Apply () or call () method is complex, and we'll talk about calling it as a function in the simplest form when we encounter it.
In general, this is also the use of
Such as:
Function Ninja () {};
Ninja ();
And their this, that is, window is called as a method
And one is called as a method in a function, this time this is the function
Such as:
var creep=function ninja () {};
var sneak={
skulk:creep
}
sneak.skulk ()//skulk This is the Sneak object
called as a constructor
When the constructor is invoked, the following behavior occurs:
Create a new empty object
The object passed to the constructor is the this argument, which becomes the constructor's function context
If no return value is displayed, the newly created object is returned as a constructor
Called as a constructor, this is the object
function Ninja () {
this.skulk=function () {return this
;
}
}
var ninja1=new Ninja ();
ASSERT (Ninja1.skulk () ===ninja1);/Through
called using the Apply () and call () methods
In the function call, JavaScript provides us with a way to show that any object is specified as its function context, and every function of JavaScript has the Apply () and call () method
Apply () pass in two parameters:
Object of the function context
An array of function arguments
The call () method uses the same method, except that the argument passed to the function is a parameter instead of a list, not a single array
Example:
function juggle () {
var result=0;
for (Var n=0;n<arguments.length;n++) {
result+=argument[n];
}
This.result=result;
}
var ninja1={};
var ninja2={};
Juggle.apply (ninja1,[1,2,3,4]);
Juggle.call (ninja2,1,2,3,4);
ASSERT (ninja1.result===10);/through
assert (ninja2.result===26);//Through
As you can see from the final test: the conclusion is correct.
NINJA1 and ninja2 the context.
The juggle method also succeeds in adding a result to the context
Of course, the final calculation is the same.