
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Object literals vs constructors in JavaScript
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.
- Related Questions & Answers
- Integer literals vs Floating point literals in C#
- Character constants vs String literals in C#
- Built-in javascript constructors?
- Type difference of character literals in C vs C++
- Tagged Template Literals in JavaScript
- Javascript Map vs Object — What and when?
- How to use a Boolean in JavaScript Constructors?
- ES6/ECMA6 template literals not working in JavaScript?
- Randomly shuffling an array of literals in JavaScript
- String Literal Vs String Object in C#
- Constructors in Java
- Constructors in C#
- Constructors in C++
- Literals in C#
- Are the constructors in an object invoked when de-serialized in Java?
Advertisements