我正在嘗試設置如下功能。從外部函數訪問屬性
function Class() {
}
Class.prototype.func = function(f) {
var hello = "hello";
setTimeout(f, 1000);
};
new Class().func(function() {
alert(hello);
});
我希望f()
函數能夠訪問hello變量。問題是f()
未在func()
函數的上下文中運行。
我試過使用var hello = "hello";
和this.hello = "hello";
,但都沒有工作。
如何才能f()
訪問hello
?
你需要傳遞一個參數。 – SLaks