JavaScript's This learning experience

Source: Internet
Author: User

This is bound at run time, not at write time, and its context depends on the various conditions of the function call. This does neither point to itself nor to the lexical scope of the function. This is the binding that occurs when a function is called, and what is pointed to exactly depends on where the function is called. This binding rule 

1. Default binding: The This binding is at the location of the function call. Only in non-strict mode can you bind to the call location, regardless of the call location in strict mode.

function foo () {console.log (this. a);} var a = 2; foo ();//2

2. Implicit binding: The This binding needs to consider whether the call location has a context object, or whether it is owned or contained by an object. This binds to the context object referenced by the function.

function foo () {    Console.log (this. a);} var obj1 = {    A:1,    Foo:foo}; var obj2 = {    A:2,    //  1

Note: The object property Reference chain has only the topmost or last layer that affects the call location.

Implicit loss: objects that are implicitly bound are lost and then default bindings are used to bind this to a global object or undefined, depending on whether it is strict mode. The root cause is that the function object passing is a reference, not a copy, unrelated to the various processes that pass through, just related to the initial definition.

function foo () {    Console.log (this. a);} var obj = {    A:0,    foo:foo}; var // function Aliases var a = "global"//  "global"

3. Explicit binding: Use the call ()/apply () method of the function for this binding. Their first argument is an object that binds the object to this.

function foo () {    Console.log (this. a);} var obj = {    A:0//  0

Call (obj,var1,var2,), apply (obj,[var1,var2,])
4. New binding: Use the new Call function (constructor call) to bind this to the newly constructed object.

function foo () {    this. A = A;}  var New foo (0); Console.log (BAR.A);   // 0
The execution process of the new operation:
    1. Create a brand-new object;
    2. The new object is executed [[prototype]] connected;
    3. The new object is bound to the this of the function call;
    4. If the function does not return another object, the function call in the new expression will automatically return the new object.
This binding judgment process
    1. Called by new? Bind to the newly created object;
    2. Called by Call/apply/bind? Binds to the specified object;
    3. Called by the context object? Bind to that context object;
    4. Default: Bind to undefined in strict mode, otherwise bind to global object.

JavaScript's This learning experience

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.