对于下面的代码,我期望控制台2和3使用“Jupiter”,但是将一个绑定到全局窗口对象,即使我传递了不同的上下文。
function otherScope () {
this.sectionHeight = "Jupiter"
}
(function () {
var sectionHeight = "Mars";
(function () {
setTimeout(function () {
console.log('console 1', sectionHeight)
})
}())
}())
window.sectionHeight = "cool!";
(function () {
setTimeout(function () {
console.log('console 2', sectionHeight)
})
}.bind(otherScope)())
setTimeout(function () {
console.log('console 3', sectionHeight)
}.bind(otherScope))
setTimeout(function () {
console.log('console 4', sectionHeight)
})
答案 0 :(得分:4)
Te otherScope是一个函数,不是具有属性的Object。你必须这样做new
。所以:
var x = new otherScope();
x.sectionHeight;
接下来,其范围仅达到当前范围。 setTimeout创建一个新范围。因此我们需要做另一个绑定。或者在setTimeout之外的第一个范围内创建一个变量。
一些例子(范围内的变量):
(function () {
var theScope = this;
setTimeout(function () {
console.log('console 2', theScope.sectionHeight)
});
}.bind(new otherScope())())
一些例子(另一个绑定):
(function () {
setTimeout(function () {
console.log('console 2', this.sectionHeight)
}.bind(this)); // Pass the context to the new scope
}.bind(new otherScope())())
答案 1 :(得分:2)
bind
为调用它的函数设置this
的值(我将其称为x
)。
它没有:
y
被 y
x
)
x
范围内的变量或x
调用的任何函数。无法更改函数所在的范围。这仅取决于声明的位置。
答案 2 :(得分:0)
Niels使用otherScope
作为构造函数和new
,这是针对您的特定问题的绝佳解决方案。但请注意,JavaScript是一种非常灵活的语言,因此有许多可能的替代方案......
你可以:
otherScope
otherScope
otherScope
等
function otherScope () {}
otherScope.sectionHeight = "Jupiter";
(function () {
var sectionHeight = "Mars";
(function () {
setTimeout(function () {
console.log('console 1', sectionHeight);
});
}());
}());
window.sectionHeight = "cool!";
(function () {
var that = this;
setTimeout(function () {
console.log('console 2', that.sectionHeight);
});
}.bind(otherScope)());
setTimeout(function () {
console.log('console 3', this.sectionHeight);
}.bind(otherScope));
setTimeout(function () {
console.log('console 4', sectionHeight);
});

function otherScope () {}
otherScope.prototype.sectionHeight = "Jupiter";
(function () {
var sectionHeight = "Mars";
(function () {
setTimeout(function () {
console.log('console 1', sectionHeight);
});
}());
}());
window.sectionHeight = "cool!";
(function () {
var that = this;
setTimeout(function () {
console.log('console 2', that.sectionHeight);
});
}.bind(otherScope.prototype)());
setTimeout(function () {
console.log('console 3', this.sectionHeight);
}.bind(otherScope.prototype));
setTimeout(function () {
console.log('console 4', sectionHeight);
});

function otherScope () {
return {sectionHeight: "Jupiter"};
}
(function () {
var sectionHeight = "Mars";
(function () {
setTimeout(function () {
console.log('console 1', sectionHeight);
});
}());
}());
window.sectionHeight = "cool!";
(function () {
var that = this;
setTimeout(function () {
console.log('console 2', that.sectionHeight);
});
}.bind(otherScope())());
setTimeout(function () {
console.log('console 3', this.sectionHeight);
}.bind(otherScope()));
setTimeout(function () {
console.log('console 4', sectionHeight);
});