🔸prototype
🚧 under construction
Last updated
Was this helpful?
🚧 under construction
Last updated
Was this helpful?
Was this helpful?
is considered outdated, modern methods are:
to access [[Prototype]]
(which is hidden and internal).
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));
// ⭐️ `__proto__` is a getter & setter for [[Prototype]]
obj.__proto__ // call getter
obj.__proto__ = proto // call setter
Object.getPrototypeOf(obj)
Object.setPrototypeOf(obj, proto)