Created
November 25, 2019 14:27
-
-
Save sag1v/654bd1e24571da23f3b6c356fad7cca7 to your computer and use it in GitHub Desktop.
Markdium-JavaScript - The prototype chain in depth
This file contains 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; | |
return newPlayer; | |
} | |
const paidPlayerFunctions = { | |
setUserName(newName) { | |
this.userName = newName; | |
} | |
} | |
// link paidPlayerFunctions object to createPlayer object | |
Object.setPrototypeOf(paidPlayerFunctions, playerFunctions); | |
function createPaidPlayer(userName, score, balance) { | |
const paidPlayer = createPlayer(name, score); | |
// we need to change the pointer here | |
Object.setPrototypeOf(paidPlayer, paidPlayerFunctions); | |
paidPlayer.balance = balance; | |
return paidPlayer | |
} | |
const player1 = createPlayer('sag1v', 700); | |
const paidPlayer = createPaidPlayer('sag1v', 700, 5); | |
console.log(player1) | |
console.log(paidPlayer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment