From 93b682df47d3f1b60d2094c94032858677985187 Mon Sep 17 00:00:00 2001 From: tjcrowder Date: Fri, 21 Aug 2009 17:41:29 +0100 Subject: [PATCH] doc: Copied Class.addMethods from current Mephisto docs and fixed minor error. [#103 state:resolved] --- src/lang/class.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/lang/class.js b/src/lang/class.js index 0733ec49b..c55268bd7 100644 --- a/src/lang/class.js +++ b/src/lang/class.js @@ -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. + * + *

Examples

+ * + * 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;