简体   繁体   English

jQuery插件-从插件内部调用公共方法

[英]jQuery Plugin - Calling a public method from within plugin

I'm having difficulty with calling a public method from within my plugin itself. 我在从插件本身内部调用公共方法时遇到困难。 I'm not sure what the issue is. 我不确定是什么问题。 What am I doing wrong? 我究竟做错了什么?

(function($) {

    var MyPlugin = function(){

        /* PUBLIC */
        this.publicFunction = function() {
            // Do public things
        };

        /* PRIVATE */
        var privateFunction = function() {
            // Do private things

            // Call this one public function
            publicFunction(); // WANT THIS but doesn't work
        }
    };

    $.fn.myPlugin = function() {

        var myPlugin = new MyPlugin(options);

        // Binding click to public function
        $("a").bind('click', function(e){
            e.preventDefault();
            myPlugin.publicFunction($(this)); // AND WANT THIS but does
        });

        return myPlugin;
    };
})(jQuery);

You just need to be able to reference the this object of MyPlugin from within the function itself without losing scope. 您只需要能够从函数本身中引用MyPlugin的this对象,而不会失去范围。 To do so assign it to a variable (name doesn't matter, typical convention is self ) 为此,将其分配给变量(名称无关紧要,典型的约定是self

var MyPlugin = function(){
    var self = this;
    ...
    var privatefunction = function () {
        self.publicFunction();
    }
...

Inside your MyPlugin function cache a reference to the MyPlugin instance: 在MyPlugin函数缓存中,对MyPlugin实例的引用:

var self = this;

then inside privateFunction call publicFunction as a member of self: 然后在privateFunction内部调用publicFunction作为self的成员:

self.publicFunction();

The problem is that you've defined publicFunction as a member of the MyPlugin instance, not as a global (which is good), but you're calling it from a different scope and since publicFunction isn't global you need to tell javascript where to find it. 问题是您已将publicFunction定义为MyPlugin实例的成员,而不是全局变量(这很好),但是您是从不同的范围调用它的,并且由于publicFunction不是全局范围,您需要告诉javascript在哪里找到它。 You can't use this because within the context of where you're calling it, this won't point to the MyPlugin instance 您不能使用this ,因为内,你叫它的背景下, this不会指向为myplugin实例

(function($) {

    var MyPlugin = function(options){

        /* PRIVATE - one copy for each instance */
        var options = $.extend({},options),
            privateFunction = function() {
                // Do private things
                // ...
                // Call this one public function
                this.publicFunction();
            };
    };

    // Shared by all MyPlugin instances;
    MyPlugin.prototype.publicFunction = function($element) {
            // Do public things
    };

    $.fn.myPlugin = function() {

        var myPlugin = new MyPlugin(options);

        // Binding click to public function
        $("a").bind('click', function(e){
            e.preventDefault();
            myPlugin.publicFunction($(this));
        });

        return myPlugin;
    };
})(jQuery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM