Create objects in JavaScript by@happymishra66

Create objects in JavaScript

May 3rd 2017 37,084 reads
Read on Terminal Reader
react to story with heart
react to story with light
react to story with boat
react to story with money
image
Rupesh Mishra HackerNoon profile picture

Rupesh Mishra

Different ways of creating objects in JS. Read on Github
Using Object literal
human object created in the previous article is an example of creating JavaScript object using object literal.
image
Using new Object() syntax
image
Creating objects using new Object() and object literal does the same thing. For simplicity, readability and execution speed, use object literal.
We can add new properties and methods to the above objects using the dot and/or square notation.
Object Constructor
Constructor function in JavaScript is used for creating new objects using a blueprint. Just like classes are used for creating objects in Java, C# we can use constructors to create objects in JavaScript.
Objects can be created using the constructor function syntax using the following two steps:
  1. Define the object blueprint(class) by defining the constructor function. By convention, name of the constructor function should start with capital letter
  2. Create the object by instantiating the constructor function using new operator
Example:
image
This is just the blueprint. To create the object we will use the new operator.
var viratKohli = new Human("Virat", "Kohli");
var sachinTendulkar = new Human("Sachin", "Tendulkar");
Another method to create object using Object.create is explained here
Other articles:

An Extensive Guide To Progressive Web Applications

  1. Let’s get this ‘this’ once and for all
  2. Service Workers
  3. Service Workers implementation
  4. Virtual DOM in ReactJS
  5. this is JavaScript
  6. Execution Context in JavaScript
  7. Prototypes in JavaScript
  8. Inheritance in JavaScript
  9. Object.create in JavaScript
  10. Objects in JavaScripts
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!
react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!