Does Javascript return a different instance object?

Tags: ,

If I use to replace each null element with an instance of object like this.

let arr = new Array(10).fill(null).map(() => new LinkedList());

Does this replace each element with the same referenced LinkedList() instance or different reference?


Usually a map function will pay attention to the existing value in the array it is being used to process so it has to be called for each item in the array and will generate a new value for each item.

const double = value => 2 * value;
const doubled = [1, 2, 3].map(double);

It isn’t called once and the value used for every position.

You’ll get a new instance for each position because the function will be called each time.

You can trivially test this with a comparison:

function LinkedList() {};
let arr = new Array(10).fill(null).map(() => new LinkedList());
console.log(arr[0] === arr[1]);

Source: stackoverflow