Movatterモバイル変換


[0]ホーム

URL:


HackerNoon LogoSia Logo
Backed up and published onsia.hackernoon.com.
Meta DataMeta Data Link
Featured

Object.create in JavaScript

Written by@happymishra66 | Published on2017-05-13T09:43:15.069Z

TL;DR →

Object.create method is another method to create new object in JavaScript.

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 theprevious article. Also, go through theprototype andinheritance article for better understanding.

Basic syntax:

Object.create(prototype_object, propertiesObject)

Object.create methods accepts two arguments.

  1. prototypeObject: Newly created objects prototype object. It has to be an object or null.
  2. 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 objectperson usingObject.create method. As we have passednull for theprototypeObject.person object does not have any prototype object.

Further, we have addedname as new property to the person object.

Create object with prototype:

Console output:

In the above example, we have created aprototypeObject withfullName function. We created aperson object withprototypeObject as prototype object of theperson’s object usingObject.create(). Further we addedfirstName andlastName properties to theperson object. Here, we have addedfirstName andlastName 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.

Object.create 2nd argument — propertiesObject

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.

Data descriptors are

  1. configurable
  2. enumerable
  3. value
  4. writable

Access descriptors are

  1. get
  2. set

In detail, descriptors can be readhere

Example:

In the above example we have created a new objectperson with prototype object asprototypeObject and properties asfirstName andlastName.

PropertiesfirstName andlastName have been added using the 2nd parameter of theObject.create().

Inheritance using Object.create()

Read Inheritance in JavaScript before reading the below part.

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

  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. Create objects in JavaScript
Written by
@happymishra66 | Software Engineer
Topics & Tags
JAVASCRIPT |OBJECT-ORIENTED |PROTOTYPE

This story onHackerNoon has a decentralized backup onSia.

Meta Data:📄


[8]ページ先頭

©2009-2025 Movatter.jp