Created
August 7, 2012 15:44
-
-
Save peterssonjesper/3286510 to your computer and use it in GitHub Desktop.
Inheritance example Javascript
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
// 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