定义的方法在我的jQuery插件返回's错误:未定义的不是一个函数

Defined method in my jQuery plugin return's the error : undefined is not a function?

本文关键字:未定义 函数 一个 错误 jQuery 我的 方法 插件 返回 定义      更新时间:2023-09-26

我有以下代码,并且我得到错误Uncaught TypeError: undefined is not a function (repeated 8 times)的方法_setProgress(),它被定义。

我的插件代码如下:

;(
function($, window, document, undefined)
{
    var pluginName          =   "imageSlider2";
    var defaults            =   {
        onImagesLoaded : undefined,
        onComplete     : undefined
    };
    var max_image_height    =   null;
    function Plugin(element, options)
    {
        this.element    =   element;
        this.settings   =   $.extend({}, defaults, options);
        this._defaults  =   defaults;
        this._name      =   pluginName;
        this._init();
    }
    Plugin.prototype = {
        _init   :   function()
        {
            $(document).on(
                'images-loaded',
                function(e)
                {
                    //_hideProgress();
                }
            );
            $(document).on(
                'slides-initiated',
                function(e)
                {
                    //_animationStart();
                }
            );
            this._hideAny();
            this._addProgress();
        },
        _hideAny    :   function()
        {
            ...
        },
        _randomID   :   function()
        {
            ...
        },
        _addProgress    :   function()
        {
            ...
            // This method just injecting an HTML element in my document
            this._imagePreload()
        },
        _setProgress    :   function(progress)
        {
            ...
        },
        _imagePreload   :   function()
        {
            ...
            $percent = 25;
            this._setProgress($percent);
            ...
        }
    };
    $.fn[pluginName]    =   function(options)
    {
        var plugin;
        this.each(
            function()
            {
                plugin  =   $.data(this,    "plugin_" + pluginName);
                if(!plugin)
                {
                    plugin  =   new Plugin(this, options);
                    $.data(this, "plugin_" + pluginName,    plugin);
                }
            }
        );
        return plugin;
    };
}
)(jQuery, window, document);

我非常怀疑,因为您重写了整个原型,因此遗漏了构造函数。最好尝试单独分配原型方法,如

 Plugin.prototype._init  = function(){}

一样设置构造函数
 Plugin.prototype.constructor  = Plugin;