是否可以使用函数调用来引用方法?
我想我可以尝试这样的事情:
function map(f,lst) {
// calling map method directly is fine.
return lst.map(f)
}
function mapm(m,lst) {
// where m is a passed method
return map( function(x) { return x.m() }, lst)
}
var list_a = [ [1,9],[2,8],[3,7],[4,6] ]
var list_b = mapm(pop,list_a)
>Uncaught ReferenceError: pop is not defined
最佳答案
尝试:
mapm( 'pop', list_a )
...
return x[ m ]();
如果您确实想引用函数本身:
mapm( list_a.pop, list_a ); // or Array.prototype.pop
...
return m.apply( x );
关于javascript - 如何引用javascript方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13924805/