Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Element.Region and Class#include
- Loading branch information
Juriy Zaytsev
committed
Aug 27, 2008
1 parent
d5a96c9
commit e042696
Showing
2 changed files
with
45 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
// include mixin into a "class" | ||
Hash.include(Enumerable); | ||
// overwrite one of the mixin methods | ||
Enumerable.each = function(){ return 'foo' }; | ||
// changes are reflected on all instances of the "class" | ||
(new Hash()).each(); // 'foo' | ||
*/ | ||
|
||
Class.Methods.include = function(mixin) { | ||
var fn; | ||
for (var prop in mixin) { | ||
// use late binding | ||
this.prototype[prop] = (function(prop){ | ||
fn = function() { | ||
return mixin[prop].apply(mixin, arguments); | ||
}; | ||
// spoof toString | ||
fn.toString = mixin[prop].toString.bind(mixin[prop]); | ||
return fn; | ||
})(prop); | ||
} | ||
return this; | ||
}; |
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