在小部件中,我试图从选项
中的属性调用函数getThing: this._runFunc()
但是我收到一个错误,说_runFunc()不是对象的实例?能帮忙吗?
$.widget('my.testW', {
options:{
buttons:buttons,
getThing: this._runFunc() // why wont _runFunc work?
},
_create: function () {
//do things
var s = this.options.getThing;
},
_runFunc: function (){
return 'hello world'
}
});
答案 0 :(得分:4)
在$.widget
调用中,options
对象及其所包含的匿名对象文字只是$.widget
的参数,因此this
指的是this
1}}在$.widget
调用之外,而不是新定义的小部件。
AFAIK,无法从该文字的值中引用同一个匿名对象文字的其他元素。
如果您希望隐藏您的功能,可以这样定义您的小部件:
(function() {
function _runFunc() {
return 'hello world';
};
$.widget(..., {
options: {
getThing: _runFunc()
},
_runFunc: _runFunc; // if you want to expose this method
});
})();
其中IIFE包含该范围内的效用函数。