Object.create()
Object.create()
is one of the 4 techniques to create a new object in JS.
It creates a new object by copying an existing object into the prototype of a new object.
This is the main concept of Object.create()
. If you remember this line then you will be able to answer any question related to this concept.
Example
1
2
3
4
5
6
7
8
9
const car = {
wheels: 4
}
const bmw = Object.create(car);
console.log(bmw); // {}
// __proto__:
// wheels: 4
The newly create object will inherit all the prototype object properties. In this case - bmw
is a newly created object
, and it inherits all the prototype object properties of car
.
Uses
Object.create()
is mainly used to achieve classical inheritance.
Interview questions
- Object.create() - Input/Ouput
- What is the difference between
Object.create()
andnew
keyword? . - How
Object.create()
is different from other object creation methods?Ans: It creates a new object by copying an existing object into the prototype of a new object.
- Difference between
var obj = Object.create(null)
andvar obj = Object.create()
?