<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - Class</title>
  <id>tag:www.prototypejs.org,2009:mephisto/api/class</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.prototypejs.org/feed/api/class/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.prototypejs.org/api/class" rel="alternate" type="text/html"/>
  <updated>2007-11-08T19:57:52Z</updated>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2007-11-08:17862</id>
    <published>2007-11-08T19:35:00Z</published>
    <updated>2007-11-08T19:57:52Z</updated>
    <category term="Class"/>
    <category term="1.6.0"/>
    <link href="http://www.prototypejs.org/api/class/addMethods" rel="alternate" type="text/html"/>
    <title>addMethods</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;addMethods(methods)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Adds methods to an existing class.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;addMethods(methods)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Adds methods to an existing class.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Class#addMethods&lt;/code&gt; is a method available on classes that have been defined with &lt;a href=&quot;/api/class/create&quot;&gt;&lt;code&gt;Class.create&lt;/code&gt;&lt;/a&gt;. It can be used to add new instance methods to that class, or overwrite existing methods, after the class has been defined.&lt;/p&gt;

&lt;p&gt;New methods propagate down the inheritance chain. If the class has subclasses, those subclasses will receive the new methods — even in the context of &lt;code&gt;$super&lt;/code&gt; calls. The new methods also propagate to instances of the class and of all its subclasses, even those that have &lt;em&gt;already&lt;/em&gt; been instantiated.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var Animal = Class.create({
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

  speak: function() {
    alert(this.name + &quot; says: &quot; + this.sound + &quot;!&quot;);
  }
});

// subclassing Animal
var Snake = Class.create(Animal, {
  initialize: function($super, name) {
    $super(name, 'hissssssssss');
  }
});

var ringneck = new Snake(&quot;Ringneck&quot;, &quot;hissssssss&quot;);
ringneck.speak();

//-&gt; alerts &quot;Ringneck says: hissssssss!&quot;

// adding Snake#speak (with a supercall)
Snake.addMethods({
  speak: function($super) {
    $super();
    alert(&quot;You should probably run. He looks really mad.&quot;);
  }
});

ringneck.speak();
//-&gt; alerts &quot;Ringneck says: hissssssss!&quot;
//-&gt; alerts &quot;You should probably run. He looks really mad.&quot;

// redefining Animal#speak
Animal.addMethods({
  speak: function() {
    alert(this.name + 'snarls: ' + this.sound + '!');
  }
});

ringneck.speak();
//-&gt; alerts &quot;Ringneck snarls: hissssssss!&quot;
//-&gt; alerts &quot;You should probably run. He looks really mad.&quot;
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-15:13068</id>
    <published>2007-01-15T23:04:00Z</published>
    <updated>2007-01-15T23:11:03Z</updated>
    <category term="Class"/>
    <link href="http://www.prototypejs.org/api/class" rel="alternate" type="text/html"/>
    <title>Class</title>
<content type="html">
            &lt;p&gt;Prototype&#8217;s object for class-based OOP.&lt;/p&gt;

&lt;p&gt;Currently, inheritance is handled through &lt;a href=&quot;/api/object/extend&quot;&gt;Object.extend&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-15:13067</id>
    <published>2007-01-15T22:53:00Z</published>
    <updated>2008-01-25T17:04:55Z</updated>
    <category term="Class"/>
    <link href="http://www.prototypejs.org/api/class/create" rel="alternate" type="text/html"/>
    <title>create</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;create([superclass][, methods...]) -&gt; Class&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates a class.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;create([superclass][, methods...]) -&gt; Class&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Creates a class.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Class.create&lt;/code&gt; returns a function that, when called, will fire its own &lt;code&gt;initialize&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Since version 1.6, &lt;code&gt;Class.create&lt;/code&gt; accepts two kinds of arguments. If the first argument is a &lt;code&gt;Class&lt;/code&gt;, 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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;$super&lt;/code&gt; as the first argument. This makes &lt;code&gt;$super&lt;/code&gt; available as a method for use within the function.&lt;/p&gt;

&lt;p&gt;To extend a class after it has been defined, use &lt;a href=&quot;/api/class/addMethods&quot;&gt;&lt;code&gt;Class.addMethods&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Special properties&lt;/h3&gt;

&lt;p&gt;Classes themselves contain several special properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;superclass&lt;/code&gt; property refers to an class's superclass (or &lt;code&gt;null&lt;/code&gt; if there is no superclass).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;subclasses&lt;/code&gt; property stores an array of all a class's subclasses (or an empty array if it has none).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition, an &lt;em&gt;instance&lt;/em&gt; of a class contains the native JavaScript &lt;code&gt;constructor&lt;/code&gt; property, which refers to the class that created it.&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var Animal = Class.create({
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

  speak: function() {
    alert(this.name + &quot; says: &quot; + this.sound + &quot;!&quot;);
  }
});

// subclassing Animal
var Snake = Class.create(Animal, {
  initialize: function($super, name) {
    $super(name, 'hissssssssss');
  }
});

var ringneck = new Snake(&quot;Ringneck&quot;);
ringneck.speak();
//-&gt; alerts &quot;Ringneck says: hissssssssss!&quot;

var rattlesnake = new Snake(&quot;Rattler&quot;);
rattlesnake.speak();
//-&gt; alerts &quot;Rattler says: hissssssssss!&quot;

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

    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');
//-&gt; alerts &quot;Ringneck says: hissssssssss!&quot;
//-&gt; alerts &quot;Rattler says: hissssssssss!&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Before 1.6&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;This is legacy documentation that applies to versions of Prototype prior to 1.6.&lt;/em&gt;&lt;/p&gt;

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

&lt;h4&gt;Example:&lt;/h4&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var Animal = Class.create();
Animal.prototype = {
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

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

var snake = new Animal(&quot;Ringneck&quot;, &quot;hissssssssss&quot;);
snake.speak();
// -&gt; alerts &quot;Ringneck says: hissssssssss!&quot;

var Dog = Class.create();

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

var fido = new Dog(&quot;Fido&quot;);
fido.speak();
// -&gt; alerts &quot;Fido says: woof!&quot;
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
</feed>
