Skip to content

Instantly share code, notes, and snippets.

@peterssonjesper
Created August 7, 2012 15:44
Show Gist options
  • Save peterssonjesper/3286510 to your computer and use it in GitHub Desktop.
Save peterssonjesper/3286510 to your computer and use it in GitHub Desktop.
Inheritance example Javascript
// Parent class with two properties, one integer and one object
var ParentClass = function() {
this.counter = 0;
this.obj = { counter : 0 };
this.inca = function() {
this.counter++;
this.obj.counter++;
}
};
// Dummy child class
var ChildClass = function() {};
// Let ChildClass inherit from ParentClass
ChildClass.prototype = new ParentClass();
ChildClass.prototype.constructor = ChildClass;
var c1 = new ChildClass();
var c2 = new ChildClass();
c1.inca();
console.log(c1.counter + ', ' + c1.obj.counter);
console.log(c2.counter + ', ' + c2.obj.counter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment