Last active
June 7, 2016 09:40
-
-
Save serafo27/1d80a4e1825af36055bb5a0af921c453 to your computer and use it in GitHub Desktop.
example of inheritance in javascript
This file contains 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
//father class | |
var classA = (function() { | |
function classA() { | |
this.val1 = "a"; | |
} | |
classA.prototype.method1 = function() { | |
console.log("my class is " + this.val1); | |
} | |
return classA; | |
}()); | |
//child class | |
var classB = (function(_super) { | |
classB.prototype = Object.create(_super.prototype); | |
function classB() { | |
_super.call(this); | |
this.val2 = "b"; | |
} | |
classB.prototype.method2 = function() { | |
this.method1(); | |
console.log("my class is " + this.val2 + " -> " + this.val1); | |
} | |
return classB; | |
}(classA)); | |
//classe figlia figlia | |
var classC = (function(_super) { | |
classC.prototype = Object.create(_super.prototype); | |
function classC() { | |
_super.call(this); | |
this.val3 = "c"; | |
} | |
classC.prototype.method2 = function() { | |
_super.prototype.method2.call(this); | |
console.log("called from class " + this.val3); | |
}; | |
classC.prototype.method3 = function() { | |
console.log("my class is " + this.val3 + " -> " + this.val2 + " -> " + this.val1); | |
}; | |
return classC; | |
}(classB)); | |
var v = new classC(); | |
v.method2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment