We present another article focusing on understanding the concept of creating objects using Object.create. The article will discuss the advantages and disadvantages of using “Object.create” to clone objects. Let’s take a look at this function in detail.
What is Object.create?
Object.create can be used to create a new object from an existing object. When we create an object using Object.create, an empty object is created, and the “proto” object is mapped to the copied object.
When the “proto” attribute matches the object to be copied, it creates a chained structure that makes the properties and functions of the old object available to the newly created object.
Let’s look at the following using an example.
var empDetails =
{
name: "Ramesh Kumar",
age: 40,
designation: "Web Developer",
contactDetails: "+91-9988776655"
}
var newEmpObject = Object.create(userDetails);
In the above example, we can see that we are trying to create a new Object using Object.create. A simple Object “empDetails” is passed to the same function. Let’s look for the debugger to look for the Object structure of new Object that is created out of this Execution.
While debugging we can observe that “newEmpObject” points out to an empty Object that contains internal “__proto__” property which maps to the “empDetails” object that is passed as a parameter to the “Object.create”.