javascript when called, in addition to the formal parameters defined at the time of the declaration, each function accepts two additional parameters: this and arguments, here is a description of the JavaScript four invocation modes and this
JavaScript calls, in addition to the formal parameters defined at the time of declaration, each function accepts two additional parameters: this and arguments,this are important in object-oriented programming, depending on the invocation pattern. JavaScript has four invocation modes, method invocation mode, function call mode, constructor invocation mode and apply invocation mode. These patterns differ in initializing the key parameter this. Method Invocation pattern: When a function is saved as an attribute of an object, we call it a method, and when a method is invoked, this is bound to the object. If the invocation expression contains a property-fetching expression (that is, a. Point expression or a script subscript expression), it is called as a method. Code as follows: var myObject = { value:0; increment:function (inc) { This.value + = typeof inc = ' Nu Mber '? inc:1; } }; myobject.increment (); Document.writeln (myobject.value);//1 Myobject.increment (2); Document.writeln (myobject.value);//2 method can use this to access an object, so it can take values from the object or modify the object. The binding of this occurs at the time of the call. This super late binding allows the function to be reused for this height. This allows you to get the context method they belong to is called a public method. Function Invocation pattern: When a function is not an object's property, it is used as a function to invoke var sum = Add (3, 4);//sum value is 7 when the function is called in this mode, this is bound to the global object. constructor Invocation pattern: JavaScript is a language based on prototype inheritance. This means that the property can be inherited directly from other objects. The language is of no class. If called with new on the front of the function, a new object is created that hides the prototype member connected to the function, and this will be bound to the new object. Apply call mode: becauseJavaScript is a function-oriented object-oriented programming language, so functions can have methods. Apply method Let's build a parameter array and use it to invoke the function. It also allows us to select the value of this. The apply method accepts two parameters, the first value to be bound to this, and the second is an array of arguments.