试图调用三种方法,但无法与jQuery map一起正常使用 [英] trying to `call` three methods but not working correctly with jQuery map
问题描述
更新#1
我忘了将当前对象作为第一个参数传递.看到K的答案
Update #1
I forgot to pass the current object as the first paremeter; see K's answer
我在Google地图上有以下方法.这些方法可以像这样正常工作:
I have the following methods that are on a google maps. The methods work fine like this:
arc.pLocations(ne, sw,m);
arc.pLikedLocations(ne, sw, m);
arc.pLikedItems(ne, sw, m);
但是,我希望将它们放置在这样的数组中,我可以对其进行子调用,但这是行不通的(我将是第一个承认这是我的问题):
However, I'd like to have them in an array like this that I can subsequetnly call but this isn't working (I'd be the first acknowledge this is an issue with me):
var selected_methods=[arc.pLocations,arc.pLikedLocations,arc.pLikedItems];
// Uncaught TypeError: Object #<Ri> has no method 'lng' - this is for a variable called sw in pLikedLocations
$.map(selected_methods, function(val, i){
console.log("here is index:" + i);
val.call(ne,sw,m);
});
试图用另一个闭包包装它,但这也不起作用
trying to wrap this with another closure but this also isn't working
$.map(selected_methods, (function(val, i){
console.log("here is index:" + i);
val.call(ne,sw,m);
})(val,i)); // Uncaught ReferenceError: val is not defined on this line!
以下是我正在做的一些非常简单的错误.谁能发现问题并为我提供帮助?这是星期六晚上,我想解决这个问题.
I have the following I'm doing something really simple wrong. Can anyone spot the issue and help me? It's saturday nigth and I want to be done with this.
提前谢谢
推荐答案
您正在将错误的上下文传递给call
函数.实际上,您会错过它.试试这个:
You're passing the wrong context to the call
function. Actually, you miss it. Try this:
var selected_methods=[arc.pLocations,arc.pLikedLocations,arc.pLikedItems];
$.map(selected_methods, function(val, i){
console.log("here is index:" + i);
val.call(arc,ne,sw,m); //Notice the arc
});
这篇关于试图调用三种方法,但无法与jQuery map一起正常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!