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
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
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
}
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
class Player {
constructor(userName, score) {
this.userName = userName;
this.score = score;
}
}
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
double.hasOwnProperty('name')
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
class Player {
constructor(userName, score) {
this.userName = userName;
this.score = score;
}
setScore(newScore) {
this.score = newScore;
}
}
@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
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
function Player(userName, score){
this.userName = userName;
this.score = score;
}
Player.prototype.setScore = function(newScore){
this.score = newScore;
}
const player1 = new Player('sag1v', 700);
@sag1v
sag1v / Markdium-JSX.jsx
Created November 25, 2019 14:27
Markdium-JavaScript - The prototype chain in depth
function Player(username, score){
if(!(this instanceof Player)){
throw new Error('Player must be called with new')
}
// ES2015 syntax
if(!new.target){
throw new Error('Player must be called with new')
}
@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",