如何调用对象内的特定函数?

问题描述 投票:0回答:1

这是我从jquery插件中获取的javascript的一部分。这个插件让我可以创建一个div小部件,里面装有远程html内容,还有一个带有刷新按钮的工具头,可以重新加载远程内容。我需要调用一个_loadAjaxFile函数来重新加载来自该插件之外的其他函数的内容(例如一个按钮),但我不能不知道这是否可能以及如何实现。

(function ($, window, document, undefined) {

    function Plugin(element, options) {
        this.obj = $(element);
        this.o = $.extend({}, $.fn[pluginName].defaults, options);
        this.objId = this.obj.attr('id');
        this.pwCtrls = '.jarviswidget-ctrls';
        this.widget = this.obj.find(this.o.widgets);
        this.toggleClass = this.o.toggleClass.split('|');
        this.editClass = this.o.editClass.split('|');
        this.fullscreenClass = this.o.fullscreenClass.split('|');
        this.customClass = this.o.customClass.split('|');
        this.storage = {enabled: this.o.localStorage};
        this.initialized = false;

        this.init();
    }

    Plugin.prototype = {

    _loadAjaxFile: function (awidget, file, loader) {

            var self = this;

            awidget.find('.widget-body')
                .load(file, function (response, status, xhr) {

                    var $this = $(this);

                    if (status == "error") {
                        $this.html('<h4 class="alert alert-danger">' + self.o.labelError + '<b> ' +
                            xhr.status + " " + xhr.statusText + '</b></h4>');
                    }

                    if (status == "success") {

                        var aPalceholder = awidget.find(self.o.timestampPlaceholder);

                        if (aPalceholder.length) {

                            aPalceholder.html(self._getPastTimestamp(new Date()));
                        }

                        if (typeof self.o.afterLoad == 'function') {
                            self.o.afterLoad.call(this, awidget);
                        }
                    }

                    self = null;
                });
            this._runLoaderWidget(loader);

        },

    }

})(jQuery, window, document);

这是插件的完整代码

enter link description here

javascript jquery function object prototype
1个回答
3
投票

关于它的_是作者告诉你不要直接调用它。这并不意味着你不能,但你不应该。您将使用不打算直接使用的内部。

我假设你引用的内容是不完整的,但是这个函数在Plugin.prototype上的事实意味着你可以在插件实例上调用它。如何获得插件的实例来调用它(以及是否可以)取决于插件(查看其文档);它可能永远不会直接向您的代码公开实例,而只是通过它添加到jQuery.fn的名称使其功能可用。

© www.soinside.com 2019 - 2024. All rights reserved.