Skip to content

Commit

Permalink
Added page on include/extend.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jan 4, 2008
1 parent 671098f commit d92020d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion site/src/layouts/application.haml
Expand Up @@ -21,7 +21,7 @@
%li
=link 'Mixins', '/mixins.html'
%li
=link '<tt>include</tt>/<tt>extend</tt>', '/includeextend.html'
=link '<tt>include</tt> and <tt>extend</tt>', '/includeextend.html'
%li
=link 'Type checking and the class tree', '/types.html'
#footer
Expand Down
46 changes: 46 additions & 0 deletions site/src/pages/includeextend.haml
@@ -0,0 +1,46 @@
:textile
h3. @include@ and @extend@

The @include@ and @extend@ directives (see "Mixins":/mixins.html) are available as static methods
on all @JS.Class@ classes. Calling @include()@ adds instance methods to the class, and @extend@
adds static methods, just like in Ruby. We could rewrite our @TodoItem@ class as:

<pre>
var TodoItem = new JS.Class({
initialize: function(position) {
this.position = position;
},

compareWith: function(other) {
if (this.position < other.position)
return -1;
else if (this.position > other.position)
return 1;
else
return 0;
}
});

TodoItem.include(Comparable);</pre>

@include@-ing/@extend@-ing a class like this will overwrite any existing methods with the same names
as those in the mixin, unless you pass @false@ as the second argument.

<pre>
TodoItem.include(Comparable, false);</pre>

If you @include()@ an object which itself has @include@ or @extend@ properties, these will be
treated as directives and applied to your class. So you can do this:

<pre>
var Enumerable = { /* methods... */ };

var Comparable = {
include: Enumerable,

/* methods... */
};

TodoItem.include(Comparable);

// TodoItem has instance methods from Enumerable and Comparable</pre>
2 changes: 1 addition & 1 deletion site/src/pages/index.haml
Expand Up @@ -6,7 +6,7 @@

The first major release is now available. The download includes:

* @class.src.js@ - human-readable source code (8.14 kb)
* @class.src.js@ - human-readable source code (8.17 kb)
* @class.js@ - a packed version (2.54 kb)
* @class.js.gz@ - a gzipped version (1.29 kb)

Expand Down

0 comments on commit d92020d

Please sign in to comment.