Skip to content

Files

Latest commit

author
Christopher Blum
Apr 11, 2011
5caf5d7 · Apr 11, 2011

History

History
36 lines (27 loc) · 1.83 KB

README.textile

File metadata and controls

36 lines (27 loc) · 1.83 KB

Class.create([superclass][, methods…])

  • 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.

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

Example

// properties are directly passed to `create` method
  var Person = Class.create({
    initialize: function(name) {
      this.name = name;
    },
    say: function(message) {
      return this.name + ': ' + message;
    }
  });
  
  // when subclassing, specify the class you want to inherit from
  var Pirate = Class.create(Person, {
    // redefine the speak method
    say: function($super, message) {
      return $super(message) + ', yarr!';
    }
  });
  
  var john = new Pirate('Long John');
  john.say('ahoy matey');
  // -> "Long John: ahoy matey, yarr!"