Object literals vs constructors in JavaScript

JavascriptFront End TechnologyObject Oriented Programming

Both new Object() notation and Object literal({}) notations do the same thing. They initialize an object. However, the second notation can be a little different if you start adding properties to it. For example,

Example

let a = {
   name: 'Ayush'
}

This initialization is equivalent to −

let a = new Object();
a.name = 'Ayush'

Or

let a = {}
a.name = 'Ayush'

This is however not the case in inherited classes. These classes have custom constructors and may modify the new ClassName() invocations to do things deviating from the above flow. That is totally at the discretion of the programmer.

raja
Published on 02-Dec-2019 11:31:41
Advertisements