Skip to content

Instantly share code, notes, and snippets.

View sag1v's full-sized avatar
🎯
Focusing

Sagiv ben giat sag1v

🎯
Focusing
View GitHub Profile
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
function PaidPlayer(userName, score, balance) {
this.balance = balance;
/* we are calling "Player" without the "new" operator
but we use the "call" method,
which allows us to explicitly pass a ref for "this".
Now the "Player" function will mutate "this"
and will populate it with the relevant properties */
Player.call(this, userName, score);
}
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
function double(num) {
return num * 2;
}
double.someProp = 'Hi there!';
double(5); // 10
double.someProp // Hi there!
double.prototype // {}
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
player1 {
userName: "sag1v",
score: 700,
__proto__: playerFunctions {
setScore: ƒ
}
}
paidPlayer {
userName: "sarah",
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
const playerFunctions = {
setScore(newScore) {
this.score = newScore;
}
}
function createPlayer(userName, score) {
const newPlayer = Object.create(playerFunctions);
newPlayer.userName = userName;
newPlayer.score = score;
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
Player {
userName: "sag1v",
score: 700,
__proto__: Player.prototype {
setScore: ƒ
}
}
PaidPlayer {
userName: "sarah",
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
Player {
userName: "sag1v",
score: 700,
__proto__: Player.prototype
}
Player {
userName: "sarah",
score: 900,
__proto__: Player.prototype
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
// link PaidPlayer.prototype object to Player.prototype object
Object.setPrototypeOf(PaidPlayer.prototype, Player.prototype);
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
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?
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
function createPlayer(userName, score) {
const newPlayer = {
userName,
score,
setScore(newScore) {
newPlayer.score = newScore;
}
}
return newPlayer;
}
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
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;
}