javascript 为什么 Array.prototype.map.call 而不是 Array.map.call
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20153455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why Array.prototype.map.call instead of Array.map.call
提问by PerrierCitror
I fell upon some lines of code where the guy uses Array.prototype.map.call
instead of Array.map.call
:
我发现了这家伙使用的一些代码行,Array.prototype.map.call
而不是Array.map.call
:
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}
Why not simply calling Array.map.call
? I checked on the Firefox console and both Array
and Array.prototype
have the map function. Is there a difference ?
为什么不简单地打电话Array.map.call
?我检查了Firefox的控制台上两者Array
并Array.prototype
有地图功能。有区别吗?
回答by Tomasz Gawel
This is because document.querySelectorAll
does not return an Array
instance but an instance of NodeList
(or at least is not guaranteed to return an Array
on all browsers).
NodeList
has indexed elements but does not include all methods from the Array
prototype.
这是因为document.querySelectorAll
不返回一个Array
实例而是一个实例NodeList
(或者至少不保证Array
在所有浏览器上都返回一个)。
NodeList
具有索引元素但不包括Array
原型中的所有方法。
This is why we need a hack calling map
method from Array
's prototype in the context of the returned object.
这就是为什么我们需要在返回对象的上下文中map
从Array
的原型中调用 hack方法的原因。
I assume that you understand that for:
我假设你明白:
var a = [], f = function() {};
the expression:
表达方式:
a.map(f);
is equivalent to:
相当于:
Array.prototype.map.call(a, f);
See also:
另见:
回答by Robbie Wxyz
Because Array.map.call
doesn't work.Array.map
is built to accept two parameters: the array, and the callback. call
runs a function setting its this
to the object you supply.
因为Array.map.call
不起作用。Array.map
被构建为接受两个参数:数组和回调。 call
运行一个函数,将其设置this
为您提供的对象。
So, when you run Array.prototype.map.call(somearray,function(){...});
it is virtually the same as if you called somearray.map(function(){...});
. Array.map
is just a utility method Javascript in Firefox only(another reason why not to use it) has to make life easier. The Array.map
function is not cross-browser.
因此,当您运行Array.prototype.map.call(somearray,function(){...});
它时,它实际上与您调用somearray.map(function(){...});
. Array.map
只是Firefox 中的一个实用方法 Javascript (不使用它的另一个原因)必须让生活更轻松。该Array.map
功能不是跨浏览器的。
Edit:The reason that they had to use Array.prototype.map.call(links,...);
instead of just links.map(...);
, is that querySelectorAll
does not return a normal array, it returns a NodeList
that does not have a map
method.
编辑:他们不得不使用Array.prototype.map.call(links,...);
而不是仅仅使用的原因links.map(...);
是它querySelectorAll
不返回普通数组,它返回一个NodeList
没有map
方法的数组。
回答by Akshay Vijay Jain
Good Question:
Array is a constructor function to create arrays.
好问题:
Array 是一个用于创建数组的构造函数。
If you type Array in browser console you will get a function definition, something like
function Array() { [native code] }
While if you type Array.prototype in browser console you will get an empty array i.e [ ]i.e. an Array object.
Consider this excerpt
如果您在浏览器控制台中键入 Array,您将获得一个函数定义,类似于function Array() { [native code] }而如果您在浏览器控制台中键入 Array.prototype,您将获得一个空数组,即[ ]即一个 Array 对象。
考虑这个摘录
function a(){
console.log('hi');
function b(){console.log('b');}
function c(){console.log('c');}
return {b:b,c:c,d:this}
}
When you type d = new a();
Then d is an object having two properties which are functions i.e. b and c and you can call>> d.b() //logs b
>> d.c() //logs c
But you cannot calla.b() or a.c()
// since function b and c is not property of a.
So just as function b and c are defined in function a. Similarly function map is defined in function Array.
So you cannot call Array.map()
but you have to get an object of Array and call map function on it. Array.prototype
gives us an Array object
Therefore they are using Array.prototype.map.call(a,func)
当你输入d = new a();
然后 d 是一个具有两个属性的对象,它们是函数,即 b 和 c,你可以调用>> d.b() //logs b
>> d.c() //logs c
但你不能调用a.b() or a.c()
// 因为函数 b 和 c 不是 a 的属性。
所以就像函数 b 和 c 在函数 a 中定义一样。类似地,函数映射是在函数数组中定义的。
所以你不能调用,Array.map()
但你必须得到一个 Array 的对象并在它上面调用 map 函数。Array.prototype
给了我们一个 Array 对象
因此他们使用Array.prototype.map.call(a,func)
Sorry for long explanation. Hope it benefits.:)
抱歉解释太长了。希望它有好处。:)
回答by Chris W.
They've probably avoided Array.map
because it doesn't exist in Chrome or IE.
他们可能已经避免了,Array.map
因为它在 Chrome 或 IE 中不存在。
回答by Dagg Nabbit
map
is meant to be called on array instances. Thus, Array.prototype.map
. Array.map
doesn't exist in most browsers.
map
旨在在数组实例上调用。因此,Array.prototype.map
。Array.map
大多数浏览器中不存在。