Mootools/Javascript 范围问题
我正在编写 Mootools 插件,但无法理解范围界定问题。一些代码来传达我的上下文:
var pluginName = new Class({
implements: [ Options ],
initialize: function(paramOne, options) {
this.setOptions(options);
},
someFunction: function() {
$$('menu').each(function(menu) {
// SCOPE OF INTEREST
});
}
};
我想知道是否有一种方法可以访问我编写的“兴趣范围”范围内的选项对象。我知道一种方法是在 someFunction 的开头设置一个变量,如下所示:
someFunction: function() {
var optionIWantToAccess = this.options.relevantOption;
$$('menu').each(function(menu) {
// now optionIWantToAccess is available here
});
}
但这似乎有点笨拙,而且闻起来有更好的选择。这个问题的一个更通用的版本是:我可以在 Mootools 迭代器内访问类级作用域(不确定这是否是正确的术语......但初始化函数内的作用域就是我所说的)?
对此的任何帮助将不胜感激。
谢谢。
I'm writing a Mootools plugin and having trouble understanding a scoping issue. Some code to convey my context:
var pluginName = new Class({
implements: [ Options ],
initialize: function(paramOne, options) {
this.setOptions(options);
},
someFunction: function() {
$('menu').each(function(menu) {
// SCOPE OF INTEREST
});
}
};
I'm wondering if there is a way to access the options object in the scope where I've written 'SCOPE OF INTEREST'. I know one way would be to set a variable at the beginning of someFunction like so:
someFunction: function() {
var optionIWantToAccess = this.options.relevantOption;
$('menu').each(function(menu) {
// now optionIWantToAccess is available here
});
}
but this seems kind of clumsy and smells of a better alternative. A more general version of this question is: Can I access the class-level scope (not sure if that's the right term....but the scope inside the initialize function is what I'm talking about) inside a Mootools iterator?
Any help on this would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将该函数绑定到实例。
Function#bind 在旧浏览器中不可用,但添加对它的支持非常简单。
You can bind the function to the instance.
Function#bind is not available in old browsers, but adding support for it is very straight-forward.