JS: Create Object with Parent X
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)
With Constructor
Use a constructor, by the following steps:
- Define a function F with no
return
statement. - Set the prototype property:
F.prototype = parentX
- Create the object.
const obj = new F
.
[see Operator “new”]
With keyword “class”
- Define a class named X. (the
constructor
should not containreturn
statement.) - JavaScript will create a object
X.prototype
. new X(…)
's parent isX.prototype
[see Class]
JavaScript, Object and Inheritance
- JS: Object Tutorial
- JS: Object Overview
- JS: Object Type
- JS: Test is Object Type 🚀
- JS: Determine Type of Object
- JS: Prototype and Inheritance
- JS: Prototype Chain
- JS: Object.prototype.isPrototypeOf
- JS: Get Set Prototype
- JS: Show Prototype Chain 🚀
- JS: Create Object
- JS: Object Literal Expression
- JS: Create Object with Parent X
- JS: Prevent Adding Property
- JS: Deep Copy Object, Array 🚀
- JS: Test Object Equality 🚀
- JS: Add Method to Prototype
- JS: Object Object
- JS: Object Constructor
- JS: Object.prototype