doc: Copied Class.addMethods from current Mephisto docs and fixed minor error. [#103 state:resolved]

This commit is contained in:
tjcrowder 2009-08-21 17:41:29 +01:00 committed by Tobie Langel
parent 63b7c5d9e8
commit 93b682df47
1 changed files with 48 additions and 0 deletions

View File

@ -81,6 +81,54 @@ var Class = (function() {
* context of `$super` calls. The new methods also propagate to instances of
* the class and of all its subclasses, even those that have already been
* instantiated.
*
* <h4>Examples</h4>
*
* var Animal = Class.create({
* initialize: function(name, sound) {
* this.name = name;
* this.sound = sound;
* },
*
* speak: function() {
* alert(this.name + " says: " + this.sound + "!");
* }
* });
*
* // subclassing Animal
* var Snake = Class.create(Animal, {
* initialize: function($super, name) {
* $super(name, 'hissssssssss');
* }
* });
*
* var ringneck = new Snake("Ringneck");
* ringneck.speak();
*
* //-> alerts "Ringneck says: hissssssss!"
*
* // adding Snake#speak (with a supercall)
* Snake.addMethods({
* speak: function($super) {
* $super();
* alert("You should probably run. He looks really mad.");
* }
* });
*
* ringneck.speak();
* //-> alerts "Ringneck says: hissssssss!"
* //-> alerts "You should probably run. He looks really mad."
*
* // redefining Animal#speak
* Animal.addMethods({
* speak: function() {
* alert(this.name + 'snarls: ' + this.sound + '!');
* }
* });
*
* ringneck.speak();
* //-> alerts "Ringneck snarls: hissssssss!"
* //-> alerts "You should probably run. He looks really mad."
**/
function addMethods(source) {
var ancestor = this.superclass && this.superclass.prototype;