JavaScript four invocation patterns and this sample _javascript tips

Source: Internet
Author: User
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 invocation pattern, 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.
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

Method can use this to access an object, so it can take values from 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 a 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 Invoke Mode: Because JavaScript is a functional object-oriented programming language, 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 one to be bound to the value of this, and the second is an array of arguments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.