Created
August 27, 2011 02:39
-
-
Save robotlolita/1174881 to your computer and use it in GitHub Desktop.
Constructors
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
// Copies properties in `properties' to `prototype' | |
function provides(prototype, properties) { | |
Object.keys(properties).forEach(function(key) { | |
prototype[key] = properties[key] }) | |
return prototype | |
} | |
// Inherits from an object | |
function inherits(ctor, base) { | |
ctor.prototype = Object.create(base) | |
ctor.prototype.constructor = ctor | |
return ctor | |
} | |
// Sample stuff | |
function Mage(name) { | |
this.name = name | |
this.job = 'Mage' | |
} provides (Mage.prototype, function() { | |
return { cast: cast } | |
function cast(magic) { | |
console.log(this.name + ' cast ' + magic) | |
} | |
}()) | |
inherits(BlackMage, Mage.prototype) | |
function BlackMage(name) { | |
Mage.call(this, name) | |
Mage.job = 'Black Mage' | |
} provides (BlackMage.prototype, function() { | |
return { fira: fira | |
, watera: watera | |
, blizarra: blizarra } | |
function fira() { this.cast('fira') } | |
function watera() { this.cast('watera') } | |
function blizarra(){ this.cast('blizarra') } | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment