当调用变量集为静态方法时,这是未定义的

this is undefined when calling variable set to static method

var Test = (function() {
    return {
        someValue: 69,
        staticMethod: function(){
          return this.someValue;
        }
    }
}());

console.log(Test.staticMethod());

var doIt = Test.staticMethod
console.log(doIt())

输出:

69
undefined

预计:

69
69

为什么 this 在第二次调用的上下文中不存在?

演示:http://jsbin.com/kidefoniko/edit?js,console

doIt 引用相同的函数 staticMethod 做:

function(){
   return this.someValue;
}

this 基本上是 . 之前的对象 在这个调用的情况下:Test.staticMethod() 它是 Test

但是这里:doIt()没有.,所以点前没有宾语。 在这种情况下 this 被分配给全局对象(如果不是在严格模式下)。全局对象(window 在浏览器中)没有 属性 调用 someValue 因此 undefined 被注销。