在Javascript闭包中访问'this'

Toj*_*oji 5 javascript

这比其他任何事情更像是一种健全性检查.我发现在使用Javascript中的闭包时,我经常使用以下模式从函数中访问封闭类:

MyClass.prototype.delayed_foo = function() {
    var self = this;
    setTimeout(function() {
        self.foo(); // Be nice if I could use 'this' here
    }, 1000);
};
Run Code Online (Sandbox Code Playgroud)

显然这种方法很好,而且使用起来也不是很麻烦.在我的脑后只有这个小小的痒,说"你让这太复杂了,假的!" 这是普遍接受的模式吗?

aar*_*ing 6

这是普遍接受的模式,that通常使用的例外代替self.