In JavaScript, the humble opinion about this point

Source: Internet
Author: User

# This point is not determined when the function is created. It can be determined only when it is executed.

# # 1, here's this point to Window Window.fn (); So This.user is undefined.

function  fn () {  var user= ' Chasing the Dreamer '; Console.log (this//   console.log (this/ / window )

# # 2, who calls, points to who

1 var o ={2     User: ' Dreamer ',3     fn:function() { 4         Console.log (this//  who calls, to who 5    }6}  7 O.fn ();    


# 3,

1 var o ={2     User: ' Dreamer ',3     fn:function() { 4         Console.log (This//  Why is not undefiend, is the window in call 5    } 6 }7//  who is calling, pointing     to who

# 4,

1 varo={2A:10,3 b:{4A:12,5Fn:function(){    6Console.log ( This. a);// A7         },8     },9 }TenO.b.fn ();

    • -If this is in the function, but he is not being skipped by the upper level object, this point points to the window
    • -If there is this in the function and this function is called by the upper-level object, this refers to the object
    • -If the function has this, and there are multiple objects inside the function, although this function is called by the outermost layer,
    • -but this point is only his upper-level object.
    • -In strict mode, this does not point to window, but undefined

# # 5, comment out a:12 this.a for undefined

1 varo={2A:10,3 b:{4         //A:12,5Fn:function(){6Console.log ( This. a); 7         },8     },9 }TenO.b.fn ();

    • -although there is no attribute a in object B, this is also point to object B, because this only points to
    • -its upper-level object, regardless of whether the object has a


# # 6, this point is always the last object to call it (execution, who calls)

1 varo={2A:10,3 b:{4A:12,5Fn:function(){6Console.log ( This. a);//undefined7Console.log ( This);//window8         },9     },Ten } One varFn=o.b.fn;//Note: This is not performed AFN ();//EXECUTE FN
    • -Note: Although the FN in the function is referenced by the object B, it is not executed when the value is assigned.
    • -So the final point is window.
    • -The above example is different, because the above example directly executes the FN ()


# # 7, this in the constructor

1 function Fn () {2     this. user= "Chasing the dream Person"; 3 }4varnew  Fn (); 5 Console.log (fn.user);
    • -The reason that object a can use UESR in the function fn is because the new keyword changes the point of this.
    • -The New keyword creates an object instance, which is equivalent to copying a copy of FN into object A.
    • -At this point, just create, no execution.
    • -The last Call is FN, then this point is naturally fn
    • -First, the New keyword creates an empty object
    • -then automatically calls the Apply method of a function, which points this to the empty object, so that the inside of the function is replaced by this empty object.
    • -Bind apply (array) call

# # 8, when the constructor return object

1 function Fn () {2     this. user= "Chasing the dream Person"; 3     return // returns an object 4 }5varnew  Fn (); 6 // undefined
    • -When an object is returned, this will point to the returned object

# 9, return simple type of data

1 function Fn () {2     this. user= "Chasing the dream Person"; 3     return // The return is a simple type 4 }5varnew  Fn (); 6 // chasing the dreamer.
    • -Returns a simple type of data, this or an instance of a pointer to a function

# # 10, return NULL when

1 function Fn () {2     this. user= "Chasing the dream Person"; 3     return NULL // The return is a simple type 4 }5varnew  Fn (); 6 // chasing the dreamer.
    • -NOTE: Returns NULL although it is an object, this is also an instance of a function, NULL special


In

JavaScript, a humble opinion of this point

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.