An Extensive Guide To Progressive Web Applications Object.create method is another method to create new object in JavaScript. Other methods of creating object in JavaScript has been described in the . Also, go through the and article for better understanding. previous article prototype inheritance Basic syntax: Object.create(prototype_object, propertiesObject) Object.create methods accepts two arguments. prototypeObject: Newly created objects prototype object. It has to be an object or null. propertiesObject: Properties of the new object. This argument is optional Create object with Object.create with no prototype Consider the below example to create a new object in JavaScript Here, we have created a new object using method. As we have passed for the . object does not have any prototype object. person Object.create null prototypeObject person Further, we have added as new property to the person object. name Create object with prototype: Console output: In the above example, we have created a with function. We created a object with as prototype object of the object using . Further we added and properties to the object. Here, we have added and properties after the object creation. It would have been great if we could add these properties while creating the object. To do that, we will use the 2nd argument of Object.create method. prototypeObject fullName person prototypeObject person’s Object.create() firstName lastName person firstName lastName Object.create 2nd argument — propertiesObject is used to create properties on new object. It acts as a descriptor for the new properties to be defined. Descriptors can be data descriptor or access descriptors. propertiesObject Data descriptors are configurable enumerable value writable Access descriptors are get set In detail, descriptors can be read here Example: In the above example we have created a new object with prototype object as and properties as and . person prototypeObject firstName lastName Properties and have been added using the 2nd parameter of the . firstName lastName Object.create() Inheritance using Object.create() before reading the below part. Read Inheritance in JavaScript Here we have copied the prototype of the SuperType to the SubType.prototype using Object.create method. Rest everything is same as the inheritance in JavaScript. Other articles: An Extensive Guide To Progressive Web Applications Let’s get this ‘this’ once and for all Service Workers Service Workers implementation Virtual DOM in ReactJS this is JavaScript Execution Context in JavaScript Prototypes in JavaScript Inheritance in JavaScript Create objects in JavaScript
Share Your Thoughts