Created
April 28, 2011 13:15
-
-
Save vestige/946323 to your computer and use it in GitHub Desktop.
tkbjs63
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 mammal = function (spec) { | |
var that = {}; | |
that.get_name = function () { | |
return spec.name; | |
}; | |
that.says = function () { | |
return spec.saying || ''; | |
}; | |
return that; | |
}; | |
var cat = function (spec) { | |
spec.saying = spec.saying || 'meow'; | |
var that = mammal(spec); | |
that.purr = function (n) { | |
var i, s = ''; | |
for (i = 0; i < n; i += 1) { | |
if (s) { | |
s += '-'; | |
} | |
s += 'r'; | |
} | |
return s; | |
}; | |
that.get_name = function () { | |
return that.says() + ' ' + spec.name + ' ' + that.says(); | |
}; | |
return that; | |
}; | |
Function.prototype.method = function (name, func) { | |
this.prototype[name] = func; | |
return this; | |
}; | |
Object.method('superior', function(name) { | |
var that = this, | |
method = that[name]; | |
return function () { | |
return method.apply(that, arguments); | |
}; | |
} ); | |
var coolcat = function (spec) { | |
var that = cat(spec), | |
super_get_name = that.superior('get_name'); | |
that.get_name = function (n) { | |
return 'like' + super_get_name() + 'baby'; | |
}; | |
return that; | |
}; | |
var myCat = cat({name: 'Henrietta'}); | |
document.writeln(myCat.get_name()); | |
document.writeln(myCat.purr(2)); | |
var myCoolcat = coolcat({name: 'Bix'}); | |
var name = myCoolcat.get_name(); | |
document.writeln(name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment