Skip to content

Commit

Permalink
doc: Copied Class.addMethods from current Mephisto docs and fixed min…
Browse files Browse the repository at this point in the history
…or error. [#103 state:resolved]
  • Loading branch information
tjcrowder authored and tobie committed Aug 22, 2009
1 parent 63b7c5d commit 93b682d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/lang/class.js
Expand Up @@ -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;
Expand Down

0 comments on commit 93b682d

Please sign in to comment.