namespace Class

Description

Manages Prototype's class-based OOP system.

Refer to Prototype's web site for a tutorial on classes and inheritance.

Methods

Class methods

  • create #

    Class.create([superclass][, methods...]) ⇒ Class
    • superclass (Class) – The optional superclass to inherit methods from.
    • methods (Object) – An object whose properties will be "mixed-in" to the new class. Any number of mixins can be added; later mixins take precedence.

    Class.create creates a class and returns a constructor function for instances of the class. Calling the constructor function (typically as part of a new statement) will invoke the class's initialize method.

    Class.create accepts two kinds of arguments. If the first argument is a Class, it's used as the new class's superclass, and all its methods are inherited. Otherwise, any arguments passed are treated as objects, and their methods are copied over ("mixed in") as instance methods of the new class. In cases of method name overlap, later arguments take precedence over earlier arguments.

    If a subclass overrides an instance method declared in a superclass, the subclass's method can still access the original method. To do so, declare the subclass's method as normal, but insert $super as the first argument. This makes $super available as a method for use within the function.

    To extend a class after it has been defined, use Class#addMethods.

    For details, see the inheritance tutorial on the Prototype website.

Instance methods

  • addMethods #

    Class#addMethods(methods) ⇒ Class
    • methods (Object) – The methods to add to the class.

    Adds methods to an existing class.

    Class#addMethods is a method available on classes that have been defined with Class.create. It can be used to add new instance methods to that class, or overwrite existing methods, after the class has been defined.

    New methods propagate down the inheritance chain. If the class has subclasses, those subclasses will receive the new methods — even in the 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."