【发布时间】:2015-08-10 00:34:31
【问题描述】:
我了解在 Javascript 对象中,this 关键字不是通过声明定义的,而是通过调用定义的。所以我想知道我们如何避免以下问题:
var testObject = function(){
this.foo = "foo!";
};
testObject.prototype.sayFoo = function() {
console.log(this.foo);
};
var test = new testObject();
test.sayFoo(); //Prints "!foo"
new Promise(function(resolve, reject){
resolve();
}).then(test.sayFoo); //Unhandled rejection TypeError: Cannot read property 'foo' of undefined
是:
new Promise(function(resolve, reject){
resolve();
}).then(function(){
test.sayFoo();
});
唯一的解决方案?
【问题讨论】:
标签: javascript bluebird