Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Created August 27, 2011 02:39
Show Gist options
  • Save robotlolita/1174881 to your computer and use it in GitHub Desktop.
Save robotlolita/1174881 to your computer and use it in GitHub Desktop.
Constructors
// 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