Created
June 19, 2017 13:48
-
-
Save mykhas/634459c181a3145368d43b8256054a4b to your computer and use it in GitHub Desktop.
Inheritance in JS
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
function A(a) { | |
this.varA = a; | |
} | |
// What is the purpose of including varA in the prototype when A.prototype.varA will always be shadowed by | |
// this.varA, given the definition of function A above? | |
A.prototype = { | |
varA: null, // Shouldn't we strike varA from the prototype as doing nothing? | |
// perhaps intended as an optimization to allocate space in hidden classes? | |
// https://developers.google.com/speed/articles/optimizing-javascript#Initializing-instance-variables | |
// would be valid if varA wasn't being initialized uniquely for each instance | |
doSomething: function() { | |
// ... | |
} | |
}; | |
function B(a, b) { | |
A.call(this, a); | |
this.varB = b; | |
} | |
B.prototype = Object.create(A.prototype, { | |
varB: { | |
value: null, | |
enumerable: true, | |
configurable: true, | |
writable: true | |
}, | |
doSomething: { | |
value: function() { // override | |
A.prototype.doSomething.apply(this, arguments); // call super | |
// ... | |
}, | |
enumerable: true, | |
configurable: true, | |
writable: true | |
} | |
}); | |
B.prototype.constructor = B; | |
var b = new B(); | |
b.doSomething(); |
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
'use strict'; | |
class Polygon { | |
constructor(height, width) { | |
this.height = height; | |
this.width = width; | |
} | |
} | |
class Square extends Polygon { | |
constructor(sideLength) { | |
super(sideLength, sideLength); | |
} | |
get area() { | |
return this.height * this.width; | |
} | |
set sideLength(newLength) { | |
this.height = newLength; | |
this.width = newLength; | |
} | |
} | |
var square = new Square(2); |
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
function Parenizor(value) { | |
this.setValue(value); | |
} | |
Parenizor.method('setValue', function (value) { | |
this.value = value; | |
return this; | |
}); | |
Parenizor.method('getValue', function () { | |
return this.value; | |
}); | |
Parenizor.method('toString', function () { | |
return '(' + this.getValue() + ')'; | |
}); | |
myParenizor = new Parenizor(0); | |
myString = myParenizor.toString(); |
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
var a = {a: 1}; | |
// a ---> Object.prototype ---> null | |
var b = Object.create(a); | |
// b ---> a ---> Object.prototype ---> null | |
console.log(b.a); // 1 (inherited) | |
var c = Object.create(b); | |
// c ---> b ---> a ---> Object.prototype ---> null | |
var d = Object.create(null); | |
// d ---> null | |
console.log(d.hasOwnProperty); | |
// undefined, because d doesn't inherit from Object.prototype |
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
let animal = { | |
eats: true | |
}; | |
let rabbit = { | |
jumps: true | |
}; | |
rabbit.__proto__ = animal; // (*) | |
// we can find both properties in rabbit now: | |
alert( rabbit.eats ); // true (**) | |
alert( rabbit.jumps ); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment