In addition to the formal parameter defined during the statement when calling, each function accepts two additional parameters: this and arguments. This is very important in object -oriented programming, depending on the call mode.
JavaScript has four call mode, method call mode, function call mode, constructor call mode, and Apply call mode. There are differences in these models on this key parameter THIS.
Method call mode: When a function is saved as an object, we call it a method. When a method is called, this is bound to the object. If the calling expression contains a attribute to take an expression (that is, one. Point expression or [script] lowering expression), then it is used as a method call.
Copy code code as follows:
var myObject = {
Value: 0;
increment: function (INC) {
this.value += typeof inc === 'number'? Inc: 1;
}
};
myObject.increment ();
document.writeln (myObject.value); // 1
MyObject.increment (2);
document.writeln (myObject.value); // 2
Methods can be used to access objects, so it can take the value or modify the object from the object. This binding occurs when calling. This super delayed binding allows the function to reuse THIS high. Through this, they can obtain their context methods called public methods.
Function call mode: When a function is not the attribute of an object, then it is used as a function to call var sm = ADD (3, 4); // SUM value is 7 to 7
When the function is called in this mode, this is bound to the global object.
Constructors call mode: JavaScript is a language -based language. This means that the attributes can be inherited directly from other objects. This language is no category.
If it is called on the front of the function, it will create a new object that hides a member connected to the function of the function. At the same time, this will be bound to that new object.
Apply call mode: Because JavaScript is a functional object -oriented programming language, the function can have methods.
The Apply method allows us to build a parameter array and use it to call the function. It also allows us to choose the value of this.
The Apply method accepts two parameters. The first value that is bound to this, and the second is a parameter array.