调用未定义
我正在尝试设置我的插件以接受内部回调函数作为选项参数:
(function($) {
$.fn.MyjQueryPlugin = function(options) {
var defaults = {
onEnd: function(e) {}
};
var settings = $.extend({}, defaults, options);
return this.each(function() {
// do stuff (complete() gets called here)
});
};
function complete(e){
settings.onEnd.call(this); // <- the error?
}
})(jQuery);
但我收到一个错误,指出 call() 未定义。我的代码有什么问题吗?
好的,我将其更改为:
(function($) {
$.fn.MyjQueryPlugin = function(options) {
var defaults = {
onEnd: function(e) {}
};
var settings = $.extend({}, defaults, options);
var complete = function(e){
settings.onEnd.call(this); // <- the error?
}
return this.each(function() {
// do stuff (complete() gets called here)
});
};
})(jQuery);
并且错误仍然存在......
I'm trying to set up my plugin to accept a callback function inside as a option argument:
(function($) {
$.fn.MyjQueryPlugin = function(options) {
var defaults = {
onEnd: function(e) {}
};
var settings = $.extend({}, defaults, options);
return this.each(function() {
// do stuff (complete() gets called here)
});
};
function complete(e){
settings.onEnd.call(this); // <- the error?
}
})(jQuery);
But I get a error that call() is undefined. What's wrong with my code?
ok, I changed this with:
(function($) {
$.fn.MyjQueryPlugin = function(options) {
var defaults = {
onEnd: function(e) {}
};
var settings = $.extend({}, defaults, options);
var complete = function(e){
settings.onEnd.call(this); // <- the error?
}
return this.each(function() {
// do stuff (complete() gets called here)
});
};
})(jQuery);
and the error is still there...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您试图在定义它的函数之外引用
settings
。您已将settings
范围限定为您分配给$.fn.MyjQueryPlugin
的函数内的局部变量,但随后您从一个不支持该变量的函数中使用它关闭该局部变量。您可以为每次调用
MyjQueryPlugin
创建一个新的complete
函数,该函数通过settings
关闭:...但是涉及创建函数的课程。也许这很好,取决于插件的作用。
或者,将
settings
作为参数传递到complete
中。You're trying to reference
settings
outside of the function in which it's defined. You've scopedsettings
to be a local variable within the function you assign to$.fn.MyjQueryPlugin
, but then you're using it from a function that doesn't close over that local variable.You could create a new
complete
function for every call toMyjQueryPlugin
that closes oversettings
:...but of course that involves creating a function. Maybe that's fine, depends on what the plug-in does.
Alternately, pass
settings
intocomplete
as an argument.settings
不在complete()
内部的范围内。settings
is not in scope inside ofcomplete()
.变量设置超出了完整函数的范围。将完整的函数放置在已定义设置的函数中。
the variable settings is out of scope in the complete function. Place the complete function in the function where you have defined settings.