- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Array.prototype.fill() with object passes reference and not new instance in JavaScript?
To fix this, you can use map() in JavaScript.
The syntax is as follows −
var anyVariableName= new Array(yourSize).fill().map(Object);
Example
Following is the code −
var arrayOfObject = new Array(5).fill().map(Object); console.log(arrayOfObject);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo311.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo311.js [ {}, {}, {}, {}, {} ]
- Related Articles
- How to implement reference to an instance method of a particular object in Java?
- How to implement an instance method of an arbitrary object using method reference in Java?
- Create new instance of an Array with Java Reflection Method
- What is the difference between `new Object()` and object literal notation in JavaScript?
- Reference to an instance method using method references in Java8
- Formatting JavaScript Object to new Array
- Create a new array from the masked array and return a new reference in Numpy
- Create new instance of a Two-Dimensional array with Java Reflection Method
- What is the difference between new operator and object() constructor in JavaScript?
- How to create a new img tag with JQuery, with the src and id from a JavaScript object?
- Removing duplicates and keep one instance in JavaScript
- How Light passes through an object?
- How to implement an instance method reference using a class name in Java?
- Remove and add new HTML Tags with JavaScript?
- How to check if an object is an instance of a Class in JavaScript?

Advertisements