JavaScript's This detailed

Source: Internet
Author: User

1. This is under global scope:

2, the general function and the strict mode of the function of this:

3. In the case of the function's this as the method of the object:

4. This on the object prototype chain:

The prototype of the object P points to O, P.a and P.a to create the properties of the object P, and THIS.A and this.b can still obtain the P.A and P.a properties on the object p when invoking the method on the prototype p;

It is important to note that P's prototype is O, which means that the call to P.F () is called the object P prototype chain o above the function property F, So the prototype chain o above this one can still get the P.A and P.a properties, that is, whether the this is the prototype chain or the P object itself, when called, this can also point to an object such as P.

5, the Get/set method under this:

6. This under the constructor:

Example 1: If you call this THIS.A normally, this will point to the global object, which will point to the window in the browser, but if you use new to call this as a constructor, this will point to an empty object, And the prototype of this empty object will point to the Myclass.propertype attribute; With new, this will point to such an empty object as the prototype's Myclass.propertype property, so since this this.a=37 is doing such an assignment, the last this will be a return value, so the object o will have a property of this o.a.
Example 2: Unlike Example 1, the C2 function returns (return) an object a:38; At this time, the value of printing out o.a is not 37, but 38; The reason: when we use new to create such a constructor to invoke, The return statement in the C2 function returns an object, so the return value is returned, so the O here is no longer the value of this object literal in return.

7, the Call/apply method under this:

This defines a variable O with two properties: A:1,b:3; then through the call () method of the Add object, the first parameter (which is the object you want to use as this), the next 5 and 7 are the arguments we want to add, and the 5 here is assigned to the C in the Add object argument. Here 7 will be assigned to the Add object in the parameters of D; The last result is 1+3+5+7=16; call and apply basically no difference, call in the way of the transfer of arguments is a flat pass through the past, and apply at the time of the transfer of parameters as an array to pass;
Note: The use of call and apply is: when we need to invoke object.prototype.toString, when we want to specify one of these

8, bind method under this: (in ie9+ only can use)

By using the Bind method,. bind () is followed by a parameter that contains an object, which is the object that you want to use as this, and then a new object is formed, in the Back Console.log (g ()). When called; This this is already pointing to this bind parameter;
The following Var o, is directly assigned to a value of a 37,f attribute directly to f (), G assignment is a bind binding method such that the output f of such a result can get 37; The above example shows that the general function, we judge by the invocation method, O.F () is called by the Call property to invoke, Then this will point to the Var O object, that is, to get the O.A is also 37, but when using bind method to bind, even if we put the new bound method as the object property to invoke, but still only according to the previous bind bind to do, and still only return test;

JavaScript's This detailed

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.