在阵列未定义的值(LEN)初始化 [英] undefined values in Array(len) initializer
问题描述
VAR一个=阵列(3);
变种B = [不确定的,不确定的,不确定]。
是什么原因,那 a.map
和 b.map
产生不同的结果?
a.map(函数(){返回0;}); //生产 - > [不确定,不确定的,不确定]
b.map(函数(){返回0;}); //生产 - > [0,0,0]
数组构造函数创建具有给定长度的数组。它的不可以创建密钥。 <一href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map\"><$c$c>Array.prototype.map$c$c>'s回调函数仅在列表中的元素执行。结果,
也就是说,这是与一个键(整数)相关联的所有值0勒; I 的&LT;的长度的
-
阵列(3)
具有零键,使.MAP
的回调是永远不会被触发。 -
[0无效,无效0,无效0]
有三个按键,为其执行的回调函数。阵列(3).hasOwnProperty(0); //假
[0无效,无效0,无效0] .hasOwnProperty(0); //真
本规范及其<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map#Compatibility\">polyfill在MDN被提及。在第47行,如果(为O k)的{
表示不存在的键不被回调函数处理。
var a = Array(3);
var b = [undefined,undefined,undefined];
What's the reason, that a.map
and b.map
produce different results?
a.map(function(){ return 0; }); //produces -> [undefined,undefined,undefined]
b.map(function(){ return 0; }); //produces -> [0,0,0]
The array constructor creates an array with the given length. It does not create the keys. Array.prototype.map
's callback function is only executed for the elements in the list.
That is, all values which are associated with a key (integer) 0 ≤ i < length.
Array(3)
has zero keys, so.map
's callback is never triggered.[void 0, void 0, void 0]
has three keys, for which the callback function is executed.Array(3).hasOwnProperty(0); // false [void 0, void 0, void 0].hasOwnProperty(0); // true
The specification and its polyfill are mentioned at MDN. At line 47, if (k in O) {
shows that non-existant keys are not treated by the callback function.
这篇关于在阵列未定义的值(LEN)初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!