forked from jcoglan/jsclass
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters