Last active
December 21, 2015 05:08
-
-
Save 8bitDesigner/6254358 to your computer and use it in GitHub Desktop.
Properly inheriting objects in Javasacript
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 Child() { | |
Parent.call(this); | |
} | |
extends(Child, Parent); |
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 extend(child, parent) { | |
for (var key in parent) { | |
if (Object.hasOwnProperty.call(parent, key)) { | |
child[key] = parent[key]; | |
} | |
} | |
function ctor() { | |
this.constructor = child; | |
} | |
ctor.prototype = parent.prototype; | |
child.prototype = new ctor; | |
child.super_ = parent.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
function extend(child, parent) { | |
child.prototype = Object.create(parent.prototype); | |
child.prototype.constructor = parent; | |
} |
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 extend(child, parent) { | |
child.super_ = parent; | |
child.prototype = Object.create(parent.prototype, { | |
constructor: { | |
value: child, | |
enumerable: false, | |
writable: true, | |
configurable: true | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment