如何在内部调用jQuery插件的方法?

a jQuery plugin like this:

var methods = {
    init: function(options) {
        $("#something").click(function() {
            //call method show
        });
    },
    show: function() {
        //show something around here.
    }
}

..

how can i call the method show inside the method init

#0

You could use proxy(), which is like bind() only it doesn't need a shim for where it's not supported...

$("#something").click($.proxy(function() {
        // `this` is the same as `this` outside the function now.
        this.show();
    }, this));

#1

You could use a closure and capture the context of the current object (this):

init: function(options) {
    var _this = this;
    $('#something').click(function() {
        _this.show();
    });
}

or you could also use the .bind() method and pass arguments to the callback\

init: function(options) {
    $('#something').bind('click', { _this: this }, function(evt) {
        evt.data._this.show();
    });
}

or directly with the .click() method:

init: function(options) {
    $('#something').click({ _this: this }, function(evt) {
        evt.data._this.show();
    });
}

#2

First, your code shows that you want to call show inside of the click event of #something.

Second, when you're executing in the context of init, unless you say methods.init.call(...) with some other object in place of ..., then this will be methods. Most likely, you will use the statement methods.init(). This is the correct assumption if you are just passing the object methods as a plugin to some jQuery widget. The following will work in this case:

    var methods = {
        init: function(options) {
            this.show();
        },
        show: function() {}
    }

Unless you want to use the click event. In that case, use:

    var methods = {
        init: function(options) {
            $('#something').click(function() {
                this.show();
            }, this);  // propogate this into click handler
        },
        show: function() {}
    }

If you expect that you will run methods.init.call(...), then you will need to make sure to set up a closure scope beforehand to keep track of the original methods object:

    var methods = function() {
        // setup closure scope
        var that = {};  // keep track of methods object
        that.init = function() {
            that.show();
        };
        that.show = function() {

        };

        // return object with closure scope... will become `methods`
        return that;
    }();

推荐文章

[Android]删除Rom中的Youtube.apk

[Android]删除Rom中的Youtube.apk

推荐文章

Linux 用户组

Linux 用户组

推荐文章

AutoHotKey是个好东西

AutoHotKey是个好东西

推荐文章

Linux安装软件的3种方式

Linux安装软件的3种方式

推荐文章

实时索引一点调研

实时索引一点调研

推荐文章

什么是SEO的真理

什么是SEO的真理

推荐文章

深入理解Glide源码,分析之路(一):基本用法,史上最详细、易懂

深入理解Glide源码,分析之路(一):基本用法,史上最详细、易懂

推荐文章

如何增加有效的外链

如何增加有效的外链

推荐文章

linux /proc 接口和共享中断

linux /proc 接口和共享中断

推荐文章

大学生如何通过一个app videoder做到日赚8万元?

大学生如何通过一个app videoder做到日赚8万元?

推荐文章

简历的加分点与失分点

简历的加分点与失分点

推荐文章

linux Tasklet 实现

linux Tasklet 实现

推荐文章

[补图]“我想要这样的输入法”

[补图]“我想要这样的输入法”

推荐文章

[译] Facebook Live如何支持80w用户同时在线

[译] Facebook Live如何支持80w用户同时在线

推荐文章

linux 安装一个共享的处理者

linux 安装一个共享的处理者

推荐文章

Facebook Software Engineer 电面面经

Facebook Software Engineer 电面面经