Skip to content

Instantly share code, notes, and snippets.

@ofca-snippets
Created December 23, 2012 20:50
Show Gist options
  • Save ofca-snippets/4366021 to your computer and use it in GitHub Desktop.
Save ofca-snippets/4366021 to your computer and use it in GitHub Desktop.
Javascript inheritance example.
/**
* Javascript inheritance example.
*/
// Parent class
function ParentClass() {
this.foo = 'foo';
};
ParentClass.prototype = {
bar: function() {
console.log('Hello from ParentClass!');
}
};
// Child class
function ChildClass() {
};
// Extend parent class
ChildClass.prototype = new ParentClass();
ChildClass.prototype.constructor = ChildClass;
// Shortcut to parent class
ChildClass.prototype.super = ParentClass.prototype;
// Overwrite parent method
ChildClass.prototype.bar = function() {
// Call parent method first
this.super.bar.call(this);
console.log('Hello from ChildClass!');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment