如何调用 javascript 对象内部的方法
我刚刚学习如何最好地组织我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您希望将
this
绑定存储在变量中。You want to store the
this
binding in a variable.迟到的答案,但 jQuery 有一个方法 称为
jQuery.proxy()
,它是为这个目的。您将函数以及要保留的this
值传递给它,它将返回一个确保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 ofthis
you want to retain, and it will return a function that ensuresthis
is correct.This way you don't need to define a variable.
您需要在
getJSON
函数外部使用对this
的变量引用。getJSON
设置 jquery 中回调的上下文。像这样:
You need to use a variable reference to
this
outside thegetJSON
function.getJSON
sets the context of the callback within jquery.Like this:
定义函数时,您只需添加 .bind(this) 即可为该函数设置正确的上下文。
when defining your function you can just add .bind(this) to set the correct context for that function.
你可以这样写:
这个类的工作方式和你一样,你仍然可以通过以下方式调用类方法:
在这个范例中,我定义了指向类本身的
self
,这样你可以在课堂上任何你想要的地方调用self
。此外,此范例可以解决您作为另一个函数的回调带来的函数中的
this
问题:You can write it likes this:
This class will works as same as you did, and you can still call the class method by:
In this paradigm, I defined
self
pointing to the class itself, so that you can callself
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: