1

I know that this is the future technology, but MDN doesn't cover all use-cases, ans almost any environment already supports this method. Here's an example I am worried about:

var groups = new Array(4);
groups.fill([]);
groups[0].push(1);

groups[0] // --> [ 1 ]
groups[1] // --> [ 1 ]

So it's not just fills groups with empty array, it fills groups with the same one empty array! Is this an intended behavior or things can change in the future?

amdc
  • 400
  • 1
  • 4
  • 17
  • 1
    `[]` is just a value like every other. `fill` won't bother cloning it, why would it? – Bergi Nov 17 '15 at 19:07
  • 1
    [`Array(4).fill(Array).map((A) => new A);`](http://www.es6fiddle.net/ih3rl7bg/) - the thing is, initialising an array of objects from an array of constructors is no less readable in ECMA-6. :-) – Emissary Nov 17 '15 at 19:24
  • @Emissary: How about `Array(4).fill(0).map(Array)`? – Bergi Nov 17 '15 at 19:29
  • @Bergi been there, tried that... `map` passes 3 unwanted parameters. ;-) – Emissary Nov 17 '15 at 19:30

1 Answers1

2

It is the intended behavior according to the spec. The value is not cloned.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143