Skip to content

Instantly share code, notes, and snippets.

@8bitDesigner
Last active December 21, 2015 05:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8bitDesigner/6254358 to your computer and use it in GitHub Desktop.
Save 8bitDesigner/6254358 to your computer and use it in GitHub Desktop.
Properly inheriting objects in Javasacript
function Child() {
Parent.call(this);
}
extends(Child, Parent);
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;
};
function extend(child, parent) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = parent;
}
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