JS: Create Object with Parent X

By Xah Lee. Date: . Last updated: .

There are several ways to create a data object with a specified parent object.

Object.Create

obj = Object.Create(parentX)

〔see Object.create

Object Literal Expression

obj = { __proto__:parentX }

(not recommended)

〔see Object.prototype.__proto__

With Constructor

Use a constructor, by the following steps:

  1. Define a function F with no return statement.
  2. Set the prototype property: F.prototype = parentX
  3. Create the object. const obj = new F.

〔see Operator “new”

With keyword “class”

  1. Define a class named X. (the constructor should not contain return statement.)
  2. JavaScript will create a object X.prototype.
  3. new X()'s parent is X.prototype

〔see Class

JavaScript, Object and Inheritance