create

create([superclass][, methods...]) -> Class

Creates a class.

Class.create returns a function that, when called, will fire its own initialize method.

Since version 1.6, Class.create accepts two kinds of arguments. If the first argument is a Class, it's treated 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 as instance methods of the new class.

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.

Special properties

Classes themselves contain several special properties:

In addition, an instance of a class contains the native JavaScript constructor property, which refers to the class that created it.

Example


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: hissssssssss!"

var rattlesnake = new Snake("Rattler");
rattlesnake.speak();
//-> alerts "Rattler says: hissssssssss!"

// mixing-in Enumerable
var AnimalPen = Class.create(Enumerable, {  
  initialize: function() {
    var args = $A(arguments);
    if (!args.all( function(arg) { return arg instanceof Animal }))
      throw "Only animals in here!"

    this.animals = args;
  },

  // implement _each to use Enumerable methods
  _each: function(iterator) {
    return this.animals._each(iterator);
  }
});

var snakePen = new AnimalPen(ringneck, rattlesnake);
snakePen.invoke('speak');
//-> alerts "Ringneck says: hissssssssss!"
//-> alerts "Rattler says: hissssssssss!"

Before 1.6

This is legacy documentation that applies to versions of Prototype prior to 1.6.

Class.create() returns a function that, when called, will fire its own initialize method. This allows for more Ruby-like OOP. It also lets you more easily subclass by overriding a parent's constructor.

Example:


var Animal = Class.create();
Animal.prototype = {
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

  speak: function() {
    alert(name + " says: " + sound + "!");
  }
};

var snake = new Animal("Ringneck", "hissssssssss");
snake.speak();
// -> alerts "Ringneck says: hissssssssss!"

var Dog = Class.create();

Dog.prototype = Object.extend(new Animal(), {
  initialize: function(name) {
    this.name  = name;
    this.sound = "woof";
  }  
});

var fido = new Dog("Fido");
fido.speak();
// -> alerts "Fido says: woof!"