陪伴而非守候
2019-06-29 09:56:08
JavaScript“New Array(N)”和“Array.Prototype.map”我在Firefox-3.5.7/Firebug-1.5.3和Firefox-3.6.16/Firebug-1.6.2中觀察到了這種情況。當(dāng)我啟動(dòng)Firebug時(shí): >>> x = new Array(3)
[undefined, undefined, undefined]
>>> y = [undefined, undefined, undefined]
[undefined, undefined, undefined]
>>> x.constructor == y.constructor
true
>>> x.map(function(){ return 0; })
[undefined, undefined, undefined]
>>> y.map(function(){ return 0; })
[0, 0, 0]What's going on here?Is this a bug, or am I misunderstanding how to usenew Array(3)?
3 回答

翻翻過去那場雪
TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
let arr = new Array(10).map((val,idx) => idx);
[0,1,2,3,4,5,6,7,8,9]
let arr = new Array(10).fill(undefined).map((val,idx) => idx);
console.log(new Array(10).fill(undefined).map((val, idx) => idx));
更新
let arr = Array.apply(null, Array(10)).map((val, idx) => idx);
console.log(Array.apply(null, Array(10)).map((val, idx) => idx));

慕尼黑8549860
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊
x = new Array(3);
y = [undefined, undefined, undefined]// The following is not equivalent to the above, it's the same as new Array(3)y = [,,,];
添加回答
舉報(bào)
0/150
提交
取消