Object.create() static method creates a new object, using an existing object as the prototype of the newly created object.
Object.create() returns a new object with the prototype set to the given object. This means that any properties or methods defined in the prototype will be available on the new object.
In JavaScript, a prototype is an internal property of an object that points to another object. Every object has a prototype except for the base object.
When a property or method is accessed on an object, JavaScript first checks if its own properties have this name; if not, it looks up the prototype chain until it either finds a property with this name or reaches an object with a null prototype.
syntax ↴
Object.create(proto, propertiesObject) ↴
proto The object which should be the prototype of the newly-created object.
propertiesObject (optional). If specified and not undefined, an object whose enumerable own properties specify property descriptors to be added to the newly-created object, with the corresponding property names ↴
These properties correspond to the second argument of Object.defineProperties()
By default properties are not writable, enumerable or configurable.
To specify a property with the same attributes as in an initializer, explicitly specify writable, enumerable and configurable.
Object.create() method allows us to create an object with null as prototype ↴
The equivalent syntax in object initializers would be the __proto__ key.
null passed as an argument means the new object would not have any prototype.
TypeError thrown if proto is neither null nor an Object.
// create an object to be used as the prototype
const player = {
team: "A",
fullName: function () {
return `${this.firstName} ${this.lastName}`
},
};
Object.create(proto, propertiesObject)