JavaScript: 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)
[see Object.prototype.__proto__]
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
- Object Basics
- Object Overview
- Object Type
- Test If a Value is Object Type 🚀
- Find Object's Type
- Prototype and Inheritance
- Prototype Chain
- Is in Prototype Chain?
- Get/Set Parent
- Show Parent Chain 🚀
- Create Object
- Object Literal Expr
- Create Object + Parent
- Prevent Adding Property
- Clone Object 🚀
- Test Object Equality 🚀
- Add Method to Prototype