Creating object with Object.create() method
The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.
Example: This example shows the implementation of the above-explained approach.
const coder = {
isStudying : false,
printIntroduction : function(){
console.log(`My name is ${this.name}. Am I studying?: ${this.isStudying}`);
}
};
const me = Object.create(coder);
me.name = 'Mukul';
me.isStudying = true;
me.printIntroduction();
Output
My name is Mukul. Am I studying?: true
Creating objects in JavaScript
A JavaScript object is a data structure containing properties and methods. Objects can be created using object literals, constructors, or classes. Properties are defined with key-value pairs, and methods are functions defined within the object, enabling encapsulation and organization of data and behavior.
These are the following 4 ways:
Table of Content
- Creating object with a constructor
- Using object literals
- Creating object with Object.create() method
- Using es6 classes