Javascript "this" pointer within nested function

I have a question concerning how the "this" pointer is treated in a nested function scenario.

Say I insert this following sample code into a web page. I get an error when I call the nested function "doSomeEffects()". I checked in Firebug and it indicates that when I am in that nested function, the "this" pointer is actually pointing to the global "window" object - which I did not expect. I must not be understanding something correctly because I thought since I declared the nested function within a function of the object, it should have "local" scope in relation to the function (ie the "this" pointer would be referring to the object itself like how it is in my first "if" statement).

Any pointers (no pun intended) would be appreciated.

var std_obj = {
  options : { rows: 0, cols: 0 },
  activeEffect : "none",
  displayMe : function() {

    // the 'this' pointer is referring to the std_obj
    if (this.activeEffect=="fade") { }

    var doSomeEffects = function() {

      // the 'this' pointer is referring to the window obj, why?
      if (this.activeEffect=="fade") { }

    }

    doSomeEffects();   
  }
};

std_obj.displayMe();

In JavaScript the this object is really based on how you make your function calls.

In general there are three ways to setup the this object:

  • someThing.someFunction(arg1, arg2, argN)
  • someFunction.call(someThing, arg1, arg2, argN)
  • someFunction.apply(someThing, [arg1, arg2, argN])
  • In all of the above examples the this object will be someThing . Calling a function without a leading parent object will generally get you the global object which in most browsers means the window object.


    this is not part of the closure scope, it can be thought of as an additional parameter to the function that is bound at the call site. If the method is not called as a method then the global object is passed as this . In the browser, the global object is identical to window . For example, consider the following funciton,

    function someFunction() {
    }
    

    and the following object,

    var obj = { someFunction: someFunction };
    

    If you call the function using method syntax such as,

    obj.someFunciton();
    

    then this is bound to obj .

    If you call someFunction() directly, such as,

    someFunction();
    

    then this is bound to the global object, that is window .

    The most common work around is to capture this into the closure such as,

    displayMe : function() {      
    
        // the 'this' pointer is referring to the std_obj      
        if (this.activeEffect=="fade") { }      
        var that = this;  
        var doSomeEffects = function() {      
    
          // the 'this' pointer is referring to global
          // that, however, refers to the outscope this
          if (that.activeEffect=="fade") { }      
        }      
    
        doSomeEffects();         
     }      
    

    由于这似乎是同类问题中最有争议的问题之一,因此,让我补充一下这些年来使用箭头函数的ES6解决方案:

    var std_obj = {
      ...
      displayMe() {
        ...
        var doSomeEffects = () => {
                            ^^^^^^^    ARROW FUNCTION    
          // In an arrow function, the 'this' pointer is interpreted lexically,
          // so it will refer to the object as desired.
          if (this.activeEffect=="fade") { }
        };
        ...    
      }
    };
    
    链接地址: http://www.djcxy.com/p/2780.html

    上一篇: JavaScript加号在函数名前面

    下一篇: 嵌套函数中的Javascript“this”指针