How to call a method inside a javascript object

后端 未结 5 1464
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 17:23

I\'m just learning about how to best organize my javascript code, and I had a question regarding this small piece of code I wrote:

var reportsControllerIndex = {         


        
5条回答
  •  南方客
    南方客 (楼主)
    2021-02-08 18:14

    Late answer, but jQuery has a method called jQuery.proxy() that is made for this purpose. You pass it the function along with the value of this you want to retain, and it will return a function that ensures this is correct.

    This way you don't need to define a variable.

    drawMap: function() {
        $.getJSON('/reports.json', $.proxy(function(data) {
            this.plotMapPoints(data);         
        }, this));
    }
    

提交回复
热议问题