JavaScript four invocation patterns and this sample

Source: Internet
Author: User
Tags constructor

 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.    
Related Article

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.