prototype vs __proto__
__proto__
refers to the object's friendkayathri
is friend ofsatya
due tosetPrototypeOf
_10var satya = { eraser: "apsara" };_10var kayathri = { pencil: "camlin", sharper: "natraj" };_10_10Object.setPrototypeOf(satya, kayathri);_10_10console.log(satya.pencil);_10// "camlin"_10_10console.log(kayathri.eraser);_10// undefined
satya.__proto__
(friend) points to kayathri
. Moreover,
satya
can use all keys of kayathri
. But, kayathri
cannot use keys of satya
An object can have only one friend
satya.__proto__
(friend) points tokayathri
satya
can use all keys ofkayathri
- But
kayathri
cannot use keys ofsatya
- An object can have only one friend
Object.prototype
is common friend of all object
_16var satya = { eraser: "apsara" };_16var kayathri = { pencil: "camlin", sharper: "natraj" };_16var badri = { books: ["eng", "tamil", "sci"] };_16_16Object.setPrototypeOf(satya, kayathri); // satya is friend of kayathri_16Object.setPrototypeOf(kayathri, badri); // kayathri is friend of badri_16_16// satya -> kayathri -> badri_16_16console.log(satya.__proto__ === kayathri); // true_16console.log(kayathri.__proto__ === badri); // true_16_16console.log(kayathri.books);_16// ["eng", "tamil", "sci"]_16console.log(satya.books);_16// ["eng", "tamil", "sci"]
_12function Account(name, accno, balance) {_12 this.name = name;_12 this.accno = accno;_12 this.balance = balance;_12 this.getBalance = function () {_12 return `The balance is: โน${this.balance}`;_12 };_12}_12_12const ragu = new Account("Ragu", 1000, 1_00_000);_12const mohamed = new Account("Mohamed", 1001, 37_00_000);_12console.log(ragu.getBalance === mohamed.getBalance); // false
_14function Account(name, accno, balance) {_14 this.name = name;_14 this.accno = accno;_14 this.balance = balance;_14}_14_14// Joint family | common friend_14Account.prototype.getBalance = function () {_14 return `The balance is: โน${this.balance}`;_14};_14_14const ragu = new Account("Ragu", 1000, 1_00_000);_14const mohamed = new Account("Mohamed", 1001, 37_00_000);_14console.log(ragu.getBalance === mohamed.getBalance); // true
@ragavkumarv
swipe to next โก๏ธ