调用未定义

发布于 2024-11-03 22:55:00 字数 999 浏览 2 评论 0原文

我正在尝试设置我的插件以接受内部回调函数作为选项参数:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提赋 2024-11-10 22:55:00

您试图在定义它的函数之外引用settings。您已将 settings 范围限定为您分配给 $.fn.MyjQueryPlugin 的函数内的局部变量,但随后您从一个不支持该变量的函数中使用它关闭该局部变量。

可以为每次调用MyjQueryPlugin创建一个新的complete函数,该函数通过settings关闭:

(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)

        });

        // `complete` now closes over `settings`
        function complete(e){
            settings.onEnd.call(this); // <- the error?
        }
    };

})(jQuery);

...但是涉及创建函数的课程。也许这很好,取决于插件的作用。

或者,将 settings 作为参数传递到 complete 中。

You're trying to reference settings outside of the function in which it's defined. You've scoped settings 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 to MyjQueryPlugin that closes over settings:

(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)

        });

        // `complete` now closes over `settings`
        function complete(e){
            settings.onEnd.call(this); // <- the error?
        }
    };

})(jQuery);

...but of course that involves creating a function. Maybe that's fine, depends on what the plug-in does.

Alternately, pass settings into complete as an argument.

寂寞陪衬 2024-11-10 22:55:00

settings 不在 complete() 内部的范围内。

settings is not in scope inside of complete().

虐人心 2024-11-10 22:55:00

变量设置超出了完整函数的范围。将完整的函数放置在已定义设置的函数中。

$.fn.MyjQueryPlugin = function(options) {
    var defaults = {
        onEnd: function(e) {}
    };

    function complete(e){
        settings.onEnd.call(this); // <- the error?
    }

    var settings = $.extend({}, defaults, options);

    return this.each(function() {
        // do stuff (complete() gets called here)

    });
};

the variable settings is out of scope in the complete function. Place the complete function in the function where you have defined settings.

$.fn.MyjQueryPlugin = function(options) {
    var defaults = {
        onEnd: function(e) {}
    };

    function complete(e){
        settings.onEnd.call(this); // <- the error?
    }

    var settings = $.extend({}, defaults, options);

    return this.each(function() {
        // do stuff (complete() gets called here)

    });
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文