如何调用 javascript 对象内部的方法

发布于 2024-10-06 13:41:45 字数 949 浏览 2 评论 0原文

我刚刚学习如何最好地组织我的 javascript 代码,并且我对我编写的这一小段代码有一个疑问:

var reportsControllerIndex = {
    plotMapPoints: function(data) {
        //plots points
    },

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

    run: function() {
        reportsControllerIndex.drawMap();
    }
};

问题是关于从 reportsControllerIndex 对象中调用 reportsControllerIndex 的另一个函数。我首先尝试了以下 run 函数的代码:

run: function() {
    this.drawMap();
}

它运行得很好。然而,我很快发现对drawMap函数这样做是

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

行不通的,因为“this”现在指的是getJSON调用的回调函数。

我的解决方案是将 reportsControllerIndex 放在我想要调用的所有方法的前面,但我很好奇:是否有一种更相对的方式来调用像这样的整个对象中的函数(就像您对中的类所做的那样)标准的面向对象语言)?或者我是否被迫像现在一样这样做,只是通过对象的名称调用方法?

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 = {
    plotMapPoints: function(data) {
        //plots points
    },

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

    run: function() {
        reportsControllerIndex.drawMap();
    }
};

The question is regarding calling another function of reportsControllerIndex from within the reportsControllerIndex object. I had first tried the following piece of code for the run function:

run: function() {
    this.drawMap();
}

which worked perfectly. However, I then quickly found doing this for the drawMap function:

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

does not work, since "this" would now refer to the callback function of the getJSON call.

My solution was to just place reportsControllerIndex in front of all of the methods I want to call, but I was curious: is there a more relative way for calling functions within an overall object like this (much like you'd do with a class in a standard OO language)? Or am I forced to do it as I am currently, just calling the methods through the name of the object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疧_╮線 2024-10-13 13:41:45

您希望将 this 绑定存储在变量中。

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

You want to store the this binding in a variable.

drawMap: function() {
    var _this = this;
    $.getJSON('/reports.json', function(data) {
        _this.plotMapPoints(data);         
    });
}
金兰素衣 2024-10-13 13:41:45

迟到的答案,但 jQuery 有一个方法 称为 jQuery.proxy() ,它是为这个目的。您将函数以及要保留的 this 值传递给它,它将返回一个确保 this 正确的函数。

这样你就不需要定义变量了。

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

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));
}
农村范ル 2024-10-13 13:41:45

您需要在 getJSON 函数外部使用对 this 的变量引用。 getJSON 设置 jquery 中回调的上下文。

像这样:

var self = this;
$.getJSON('/reports.json', function(data) {
    self.plotMapPoints(data);         
});

You need to use a variable reference to this outside the getJSON function. getJSON sets the context of the callback within jquery.

Like this:

var self = this;
$.getJSON('/reports.json', function(data) {
    self.plotMapPoints(data);         
});
梦年海沫深 2024-10-13 13:41:45
plotMapPoints: function(data) {
    //plots points
}.bind(this)

定义函数时,您只需添加 .bind(this) 即可为该函数设置正确的上下文。

plotMapPoints: function(data) {
    //plots points
}.bind(this)

when defining your function you can just add .bind(this) to set the correct context for that function.

追我者格杀勿论 2024-10-13 13:41:45

你可以这样写:

var reportsControllerIndex = new function () {

    var self = this;

    self.plotMapPoints = function (data) {
        //plots points
    },

    self.drawMap = function () {
        $.getJSON('/reports.json', function (data) {
            self.plotMapPoints(data);         
        });
    },

    self.run = function () {
        self.drawMap();
    }
};

这个类的工作方式和你一样,你仍然可以通过以下方式调用类方法:

reportsControllerIndex.run()

在这个范例中,我定义了指向类本身的 self ,这样你可以在课堂上任何你想要的地方调用 self


此外,此范例可以解决您作为另一个函数的回调带来的函数中的 this 问题:

plotMapPoints: function(data) {
    console.log(this);
    // Need a this referring to the class itself
    // It's inconvenient to bring this as parameter
},

You can write it likes this:

var reportsControllerIndex = new function () {

    var self = this;

    self.plotMapPoints = function (data) {
        //plots points
    },

    self.drawMap = function () {
        $.getJSON('/reports.json', function (data) {
            self.plotMapPoints(data);         
        });
    },

    self.run = function () {
        self.drawMap();
    }
};

This class will works as same as you did, and you can still call the class method by:

reportsControllerIndex.run()

In this paradigm, I defined self pointing to the class itself, so that you can call self wherever you want in the class.


Farther, this paradigm can solve the this problem in the function that you bring as callback to another funciton:

plotMapPoints: function(data) {
    console.log(this);
    // Need a this referring to the class itself
    // It's inconvenient to bring this as parameter
},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文