jquery 插件,布尔回调参数

jquery plugin, boolean callback parameter

我试图从 jquery 插件中将布尔参数传递给回调,但参数始终未定义。

每次单击 link 时,onSwitch 回调参数应在 true 和 false 之间交替。使用调试器,我可以看到传递给回调调用函数的值被正确定义为 true 或 false,但在回调实现中它变为未定义。

我已经尝试查看其他几个类似的问题,例如 this thisthis 但似乎无法让它发挥作用。

这是我的插件定义:

(function ($) {
    $.fn.switcherButton = function (options) {
        // Set the default options
        var settings = $.extend({},$.fn.switcherButton.defaults, options);

        this.click(function () {
            $.fn.switcherButton.switched = !$.fn.switcherButton.switched;
            settings.onSwitch.call($.fn.switcherButton.switched);
        });
        return this;
    };
    // Plugin defaults – added as a property on our plugin function.
    $.fn.switcherButton.defaults = {
        onSwitch: function() {}
    };
    $.fn.switcherButton.switched = false;
}(jQuery));

HTML:

<a id="switchTest" href="#">switch</a>

插件初始化:

$("#switchTest").switcherButton({
    onSwitch: function(switched){
        if(typeof switched === "undefined")
            alert("callback param = undefined");
        else
            if(switched)
                alert("callback param = true");
            else
                alert("callback param = false");
    }
});

我已经创建了问题的 jsfiddle here

settings.onSwitch.call($.fn.switcherButton.switched)改为settings.onSwitch($.fn.switcherButton.switched)

调用用于设置 this 而不是函数的实际参数