Created
June 3, 2010 21:30
-
-
Save bnolan/424525 to your computer and use it in GitHub Desktop.
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, Horse, Snake, sam, tom; | |
var __extends = function(child, parent) { | |
var ctor = function(){ }; | |
ctor.prototype = parent.prototype; | |
child.__superClass__ = parent.prototype; | |
child.prototype = new ctor(); | |
child.prototype.constructor = child; | |
}; | |
Animal = function() { | |
}; | |
Animal.prototype.move = function(meters) { | |
return console.log(this.name + " moved " + meters + "m."); | |
}; | |
Animal.Mixin = function(self){ | |
var sex = "male"; | |
self.getSex = function(){ | |
return sex; | |
} | |
self.thinkThenMove = function(){ | |
console.log("thinking..."); | |
self.move(); | |
} | |
} | |
Snake = function(name) { | |
new Animal.Mixin(this); | |
this.name = name; | |
return this; | |
}; | |
__extends(Snake, Animal); | |
Snake.prototype.move = function() { | |
console.log("Slithering..."); | |
return Snake.__superClass__.move.call(this, 5); | |
}; | |
sam = new Snake("Sammy the snake"); | |
// sam.sex => undefined | |
// sam.getSex => 'male' | |
// sam instanceof Animal => true | |
// sam instanceof Snake => true | |
// sam.thinkThenMove() => slithering... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment