This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Player { | |
| userName: "sag1v", | |
| score: 700, | |
| __proto__: Player.prototype | |
| } | |
| Player { | |
| userName: "sarah", | |
| score: 900, | |
| __proto__: Player.prototype |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Player { | |
| constructor(userName, score) { | |
| this.userName = userName; | |
| this.score = score; | |
| } | |
| setScore(newScore) { | |
| this.score = newScore; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| double.hasOwnProperty('name') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Player { | |
| constructor(userName, score) { | |
| this.userName = userName; | |
| this.score = score; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Player(userName, score){ | |
| this = {} // ⚠️ done by JavaScript | |
| this.__proto__ = Player.prototype // ⚠️ done by JavaScript | |
| this.userName = userName; | |
| this.score = score; | |
| return this // ⚠️ done by JavaScript | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const playerFunctions = { | |
| setScore(newScore) { | |
| this.score = newScore; | |
| } | |
| } | |
| function createPlayer(userName, score) { | |
| const newPlayer = Object.create(playerFunctions); | |
| newPlayer.userName = userName; | |
| newPlayer.score = score; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PaidPlayer extends Player { | |
| constructor(userName, score, balance) { | |
| // "this" is uninitialized yet... | |
| // super refers to Player in this case | |
| super(userName, score); | |
| // under the hood super is implemented with Reflect.construct | |
| // this = Reflect.construct(Player, [userName, score], PaidPlayer); | |
| this.balance = balance; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function createPlayer(userName, score) { | |
| const newPlayer = { | |
| userName, | |
| score, | |
| setScore(newScore) { | |
| newPlayer.score = newScore; | |
| } | |
| } | |
| return newPlayer; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function double(num){ | |
| return num * 2; | |
| } | |
| double.toString() // where is this method coming from? | |
| Function.prototype // {toString: f, call: f, bind: f} | |
| double.hasOwnProperty('name') // where is this method coming from? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // link PaidPlayer.prototype object to Player.prototype object | |
| Object.setPrototypeOf(PaidPlayer.prototype, Player.prototype); |