Understanding of the This keyword for nested functions in JavaScript

Source: Internet
Author: User

As we all know, in a method invocation, this refers to the object to which the method belongs, such as the following code:

Obj.test (Console.log (this// returns True

However, in nested functions, the situation has changed a lot. This needs to be divided into two situations: strict mode and non-strict mode

 //   obj = {test:  function   () { var  self = this  ; Console.log ( this  === obj);        True f ();  function   F () {Console.log ( 
     
      this 
      === obj); False Console.log (self  === obj);  True Console.log ( this  === window);        True}; }};obj.test ();  
//under strict mode"Use Strict"varobj ={test:function (){        varSelf = This; Console.log ( This===obj);        True f (); functionf () {Console.log ( This===obj); False Console.log ( self===obj); True Console.log ( This===window); False Console.log ( This===undefined);        True}; }};obj.test ();

Understanding of the This keyword for nested functions in JavaScript

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.