开发者

Is there a way to pass context to bind in jQuery?

I'm inside a javascript object (vr roxx :) ), but every time I do an event bind with jQuery I have to include the main object instance's context through the data parameter in order to work with it. Isn't there an ea开发者_Python百科sy/neat way to do this in jQuery?

var oink = 
{
    pig: null,

    options:    
    {
        showPigMom: 0
    },

    init: function(pigObj)
    {

        //Show the pigmom
        $(this.doc).bind('keyup click', {o: this}, function(e)
        {
            var o = e.data.o;
            if (o.options.showpath)
                o.doWhatever();
        });

    ...


I use the $.proxy() function

init: function(pigObj)
{
    //Show the pigmom
    $(this.doc).bind('keyup click', $.proxy(function(e) {
        if (this.options.showpath)
            this.doWhatever();
        $(e.currentTarget).text(); // use this to access the clicked element
    }, this));
}


init: function() {
    var self = this;
    $(this.doc).bind('keyup click', function() {
        if (self.options.showpath) self.doWhatever();
    });
}


init: function() {
    $(this.doc).bind('keyup click', function() {
       if (this.options.showpath) this.doWhatever();
       $(e.currentTarget).text(); // use this to access the clicked element
    }.bind(this))
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜