Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Much better English wording
Source Link
Hux
  • 1.5k
  • 1
  • 9
  • 6

There is a hugean important difference that no oneanswer has mentioned yet.

You might think the new Array(2) is equivalent to [undefined, undefined] before because we haveFrom this:

new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true

You might think the new Array(2) is equivalent to [undefined, undefined], BUT IT'Sbut it's NOT!

Let's try with map():

[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined ×ばつ 2]" in Chrome

See? It'sThe semantics are totally different! ButSo why is that?

According to ES6 Spec 22.1.1.2, the job of Array(len) only createsis just creating a new array withwhose property length is set to the argument len and nothing more. Thusthat's it, meaning there is no real elementisn't any real element inside the newthis newly created array.

While functionFunction map(), according to spec 22.1.3.15 would firstly check HasProperty then call the callback, but it turns out that:

new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true

That'sAnd that's why you can not expect any iterationiterating functions workworking as usual to arrayon arrays created from new Array(len).

BTW, Safari and Firefox have a much better expression"printing" to this situation:

// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2) 
[undefined, undefined] // [undefined, undefined] (2) 
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]

I have already submitted an issue to Chrome toChromium and ask them to fix this confusing logprinting: https://bugs.chromium.org/p/chromium/issues/detail?id=732021

UPDATE: It's already fixed. Chrome now logprinted as:

new Array(2) // (2) [empty ×ばつ 2]

There is a huge difference that no one mentioned.

You might think the new Array(2) is equivalent to [undefined, undefined] before because we have

new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true

BUT IT'S NOT!

Let's try map():

[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined ×ばつ 2]" in Chrome

See? It's different! But why is that?

According to ES6 Spec 22.1.1.2, Array(len) only creates a new array with length set to len and nothing more. Thus there is no real element inside the new array.

While function map(), according to spec 22.1.3.15 would firstly check HasProperty then call the callback, but it turns out that:

new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true

That's why you can not expect any iteration functions work as usual to array created from new Array(len).

BTW, Safari and Firefox have a much better expression to this:

// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2) 
[undefined, undefined] // [undefined, undefined] (2) 
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]

I have already submitted an issue to Chrome to ask them to fix this confusing log: https://bugs.chromium.org/p/chromium/issues/detail?id=732021

UPDATE: It's already fixed. Chrome now log as

new Array(2) // (2) [empty ×ばつ 2]

There is an important difference that no answer has mentioned yet.

From this:

new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true

You might think the new Array(2) is equivalent to [undefined, undefined], but it's NOT!

Let's try with map():

[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined ×ばつ 2]" in Chrome

See? The semantics are totally different! So why is that?

According to ES6 Spec 22.1.1.2, the job of Array(len) is just creating a new array whose property length is set to the argument len and that's it, meaning there isn't any real element inside this newly created array.

Function map(), according to spec 22.1.3.15 would firstly check HasProperty then call the callback, but it turns out that:

new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true

And that's why you can not expect any iterating functions working as usual on arrays created from new Array(len).

BTW, Safari and Firefox have a much better "printing" to this situation:

// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2) 
[undefined, undefined] // [undefined, undefined] (2) 
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]

I have already submitted an issue to Chromium and ask them to fix this confusing printing: https://bugs.chromium.org/p/chromium/issues/detail?id=732021

UPDATE: It's already fixed. Chrome now printed as:

new Array(2) // (2) [empty ×ばつ 2]
Fixed grammar, Update info
Source Link
Hux
  • 1.5k
  • 1
  • 9
  • 6

There is a huge difference that no one mentioned.

You maymight think the new Array(2) is equivalent to [undefined, undefined] cuzbefore because we have

new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true

BUT IT'S NOT!

Let's try map():

[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined ×ばつ 2]" in Chrome

See? It's different! But why is that?

According to ES6 Spec 22.1.1.2, Array(len) only createcreates a new array with length set to len and nothing more. Thus there is no real element inside the new array. And

While function map(), according to spec 22.1.3.15 would firstly check HasProperty then call the callback, but it turns out the array hasn'tthat:

new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true

That's why you can not expect any iteration functions work the sameas usual to array created from new Array(len).

BTW, Safari and Firefox have a much better expression to this:

// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2) 
[undefined, undefined] // [undefined, undefined] (2) 
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]

And I have already submitted an issue to Chrome to ask them to fix this confusing log: https://bugs.chromium.org/p/chromium/issues/detail?id=732021

UPDATE: It's already fixed. Chrome now log as

new Array(2) // (2) [empty ×ばつ 2]

There is a huge difference that no one mentioned.

You may think the new Array(2) is equivalent to [undefined, undefined] cuz we have

new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true

BUT IT'S NOT!

Let's try map():

[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined ×ばつ 2]" in Chrome

See? It's different! But why is that?

According to ES6 Spec 22.1.1.2, Array(len) only create a new array with length set to len and nothing more. Thus there is no real element inside the new array. And map, according to spec 22.1.3.15 would firstly check HasProperty but turns out the array hasn't:

new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true

That's why you can not expect any iteration functions work the same to new Array(len).

BTW, Safari and Firefox have a much better expression to this:

// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2) 
[undefined, undefined] // [undefined, undefined] (2) 
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]

And I have submitted an issue to Chrome to ask them to fix this confusing log: https://bugs.chromium.org/p/chromium/issues/detail?id=732021

There is a huge difference that no one mentioned.

You might think the new Array(2) is equivalent to [undefined, undefined] before because we have

new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true

BUT IT'S NOT!

Let's try map():

[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined ×ばつ 2]" in Chrome

See? It's different! But why is that?

According to ES6 Spec 22.1.1.2, Array(len) only creates a new array with length set to len and nothing more. Thus there is no real element inside the new array.

While function map(), according to spec 22.1.3.15 would firstly check HasProperty then call the callback, but it turns out that:

new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true

That's why you can not expect any iteration functions work as usual to array created from new Array(len).

BTW, Safari and Firefox have a much better expression to this:

// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2) 
[undefined, undefined] // [undefined, undefined] (2) 
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]

I have already submitted an issue to Chrome to ask them to fix this confusing log: https://bugs.chromium.org/p/chromium/issues/detail?id=732021

UPDATE: It's already fixed. Chrome now log as

new Array(2) // (2) [empty ×ばつ 2]
Source Link
Hux
  • 1.5k
  • 1
  • 9
  • 6

There is a huge difference that no one mentioned.

You may think the new Array(2) is equivalent to [undefined, undefined] cuz we have

new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true

BUT IT'S NOT!

Let's try map():

[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined ×ばつ 2]" in Chrome

See? It's different! But why is that?

According to ES6 Spec 22.1.1.2, Array(len) only create a new array with length set to len and nothing more. Thus there is no real element inside the new array. And map, according to spec 22.1.3.15 would firstly check HasProperty but turns out the array hasn't:

new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true

That's why you can not expect any iteration functions work the same to new Array(len).

BTW, Safari and Firefox have a much better expression to this:

// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2) 
[undefined, undefined] // [undefined, undefined] (2) 
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]

And I have submitted an issue to Chrome to ask them to fix this confusing log: https://bugs.chromium.org/p/chromium/issues/detail?id=732021

lang-js

AltStyle によって変換されたページ (->オリジナル) /