jQuery.proxy() usage

后端 未结 3 877
执念已碎
执念已碎 2020-11-28 03:11

I was reading the api about jQuery.proxy(). It looks promising but I was wondering in what situation is this best use. Can anyone enlighten me?

3条回答
  •  半阙折子戏
    2020-11-28 03:31

    For example if you want to create callbacks. Instead of:

    var that = this;
    
    $('button').click(function() {
        that.someMethod();
    });
    

    you can do:

    $('button').click($.proxy(this.someMethod, this));
    

    Or if you create a plugin that accepts callbacks and you have to set a specific context for the callback.

提交回复
热议问题