🔸prototype
🚧 under construction
JS ⟩ value ⟩ object ⟩ prototype
desc
__proto__
is a getter/setter for [[Prototype]] and resides in Object.prototype.
is considered outdated, modern methods are:
to access
[[Prototype]](which is hidden and internal).
// ⭐️ `__proto__` is a getter & setter for [[Prototype]]
obj.__proto__ // call getter
obj.__proto__ = proto // call setter
Object.getPrototypeOf(obj)
Object.setPrototypeOf(obj, proto)prototype chain can't go in circles.
IteratorPrototype - prototype of all built-in iterators.
all objects created by object literal have the same prototype - Object.prototype.
used in object's prototypal inheritance.
function's prototype is used as new 's [[Prototype]].
super ===
[[HomeObject]].[[Prototype]]⭐️[[Prototype]] is also cloned by clone(obj).
"pure" object is an object without [[Prototype]] (=== null).
const { log } = console;
// ------------------------ user ---------------------------
let user = {
// instance properties
name : 'John',
surname: 'Smith',
// setter/getter
set fullName(value) {
[this.name, this.surname] = value.split(' ');
},
get fullName() {
return `${this.name} ${this.surname}`;
}
};
// -------------------------- admin -------------------------
let admin = {
__proto__: user, // ⭐ admin.__proto__ === user
isAdmin: true,
};
// ------------------------- test --------------------------
[
admin.fullName, // 📤 get: "John Smith" (from `admin.__proto__`❗)
admin.fullName = 'Alice Cooper', // 🖊️ set: "Alice Cooper" (this === admin❗)
admin.fullName, // 📤 get: "Alice Cooper" (from `admin`❗)
user.fullName, // 📤 get: "John Smith" (from `user`, this === user❗)
].forEach(x => log(x));Last updated
Was this helpful?