将变量注入回调函数范围 [英] Inject variable into callback function scope
本文介绍了将变量注入回调函数范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可以将变量添加到回调范围吗?我想要实现的是:
Is this possible to add variable to callback scope? What I want to achieve is:
...
Foo.prototype.bar = function(fn) {
var baz = "baz!";
fn.call(this);
}
...
Foo.bar(function() {
console.log(baz) // gives "baz!"
});
我知道我可以将 baz
变量作为参数或<$ c $传递c>这个但我对上面的内容很感兴趣。
I know I can pass baz
variable as an argument or this
but I'm interested in something like above.
推荐答案
不,这是不可能的。唯一的方法是你指出的:作为一个参数或在这个
。
No, it's not possible. The only ways are the ones you pointed out: as an argument or in this
.
这篇关于将变量注入回调函数范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文