Yes, it's a shallow clone issue. You can test this in your browser console (create a New Tab, and go to the browser's Developer Console):
const a = { b: { c: 'hello world' } };
const b = Object.create(a);
console.log(b); // {}
You need to use a deep-clone technique in order to recreate your object.
Answer from sfdcfox on Stack ExchangeYes, it's a shallow clone issue. You can test this in your browser console (create a New Tab, and go to the browser's Developer Console):
const a = { b: { c: 'hello world' } };
const b = Object.create(a);
console.log(b); // {}
You need to use a deep-clone technique in order to recreate your object.
The accepted answer here is actually incorrect. It is not a shallow clone issue. The reason b is showing as an empty object is because of prototypal inheritance. if you were to console.log(b.__proto__), you'd see that those properties are on the prototype and both b.b as well as b.b.c are accessible.
[jQuery] Creating an empty jQuery object
arrays - Create an empty object in JavaScript with {} or new Object()? - Stack Overflow
Adding empty properties to an object
How to initialise an empty object?
Videos
Objects
There is no benefit to using new Object(), whereas {} can make your code more compact, and more readable.
For defining empty objects they're technically the same. The {} syntax is shorter, neater (less Java-ish), and allows you to instantly populate the object inline - like so:
var myObject = {
title: 'Frog',
url: '/img/picture.jpg',
width: 300,
height: 200
};
Arrays
For arrays, there's similarly almost no benefit to ever using new Array() over [] — with one minor exception:
var emptyArray = new Array(100);
creates a 100 item long array with all slots containing undefined, which may be nice/useful in certain situations (such as (new Array(9)).join('Na-Na ') + 'Batman!').
My recommendation
- Never use
new Object();— it's clunkier than{}and looks silly. - Always use
[]— except when you need to quickly create an "empty" array with a predefined length.
Yes, There is a difference, they're not the same. It's true that you'll get the same results but the engine works in a different way for both of them. One of them is an object literal, and the other one is a constructor, two different ways of creating an object in javascript.
var objectA = {} //This is an object literal
var objectB = new Object() //This is the object constructor
In JS everything is an object, but you should be aware about the following thing with new Object(): It can receive a parameter, and depending on that parameter, it will create a string, a number, or just an empty object.
For example: new Object(1), will return a Number. new Object("hello") will return a string, it means that the object constructor can delegate -depending on the parameter- the object creation to other constructors like string, number, etc... It's highly important to keep this in mind when you're managing dynamic data to create objects..
Many authors recommend not to use the object constructor when you can use a certain literal notation instead, where you will be sure that what you're creating is what you're expecting to have in your code.
I suggest you to do a further reading on the differences between literal notation and constructors on javascript to find more details.
I have following scenario. I have an array of objects. I loop through the array and based on value of one object i want to initialise an object. Whats the best approach to do this.
Note: I initialised the object with empty string and it works but I wanted to know if there is any better approach.