This is the 9th day of my participation in the August More Text Challenge

Object’s Prototype property specifies the object from which it inherits properties. This is a very important feature, and we usually only talk about archetypes of O, not archetypes of O. Also remember that when Prototype appears in code, it refers to a generic object property, not a Prototype feature.

The stereotype feature is set when an object is created. Objects created with Object literals use Object.prototype as their prototype. An object created with new uses the value of its constructor’s Prototype property as its prototype. And objects created with Object.create() take the function’s first argument (possibly null) as their prototype.

You can query the prototype of any Object by passing it to Object.getProtoTypeof () :

Object.getPrototypeOf({}) // => Object.prototype
Object.getPrototypeOf([]) // => Array.prototype
Object.getPrototypeOf(() = >{}) // => Function.prototype
Copy the code

To determine if an object is a prototype (or part of a chain of prototypes) of another object, use the isPrototypeOf() method:

let p = {x: 1}; // Define a prototype object.
let o = Object.create(p); // Create an object with this prototype.
p.isPrototypeOf(o) // => true: o inherits from P
Object.prototype.isPrototypeOf(p) // => true: p inherits from Object.prototype
Object.prototype.isPrototypeOf(o) // => true: o is also true
Copy the code

IsPrototypeOf () performs a function similar to the instanceof operator.

The stereotype nature of an object is set when the object is created and usually remains the same. However, you can change the prototype of an Object using object.setPrototypeof () :

let o = {x: 1};
let p = {y: 2};
Object.setPrototypeOf(o, p); // Set the prototype of o to p
o.y // => 2: o now inherits y
let a = [1.2.3];
Object.setPrototypeOf(a, p); // Prototype array A to p
a.join // => undefined: A no longer has the join() method
Copy the code

Object.setprototypeof () is generally not required. JavaScript implementations may be actively optimized based on the assumption that an object’s prototype is fixed. This means that if you call Object.setPrototypeof (), any code that uses a changed Object is likely to run much slower than normal.

Some early browser implementations of JavaScript exposed the prototypical nature of objects through the __proto__ attribute. This is long out of date, but enough code on the Web already relies on __proto__ that the ECMAScript standard forces Javascript-enabled implementations of Web browsers to support this property. (Node also supports it, although the standard does not require it for Node.) In modern JavaScript, __proto__ is both readable and writable and can (though should not) be used as an alternative to Object.getPrototypeof () and object.setPrototypeof (). However, an interesting use of __proto__ is to define the prototype of an object literal:

let p = {z: 3};
let o = {
 x: 1.y: 2.__proto__: p
};
o.z // => 3: O inherits from P
Copy the code