Created
April 3, 2014 09:06
-
-
Save telekosmos/9951046 to your computer and use it in GitHub Desktop.
Combination of Functional/Prototypal Inheritance
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
var Animal = function () { | |
var helloCount = 0; | |
var self = this; | |
//priviledge methods | |
this.AnimalHello = function() { | |
helloCount++; | |
console.log(self.Name + ' says hello (animalHello)'); | |
}; | |
this.GetHelloCount = function (callback) { | |
callback.call(null, helloCount); | |
} | |
}; | |
Animal.prototype = { | |
Name: 'Generic', | |
IsAnimal: true | |
}; | |
var Sheep = function (name) { | |
var sheep = new Animal(); | |
//use parasitic inheritance to extend sheep | |
//http://www.crockford.com/javascript/inheritance.html | |
sheep.Name = name || 'Woolie' | |
sheep.SheepHello = function() { | |
this.AnimalHello(); | |
var self = this; | |
this.GetHelloCount(function(count) { | |
console.log('i (' + self.Name + ') have said hello ' + count + ' times (sheepHello anon callback)'); | |
}); | |
} | |
return sheep; | |
}; | |
Sheep.prototype = new Animal(); | |
Sheep.prototype.isSheep = true; | |
var sheepie = new Sheep('Sheepie'); | |
var lambie = new Sheep('Lambie'); | |
sheepie.AnimalHello(); | |
sheepie.SheepHello(); | |
lambie.SheepHello(); |
Refer http://webreflection.blogspot.com.es/2010/02/javascript-override-patterns.html for a class provider with prototypical inheritance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From http://stackoverflow.com/questions/16659326/simple-javascript-inheritance-using-extend-and-module-pattern