Created
August 10, 2015 14:53
-
-
Save egoist/8544c420a852f8c054ac to your computer and use it in GitHub Desktop.
Operator Overloading
This file contains hidden or 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
| var y, words, unspaced, split$ = ''.split, replace$ = ''.replace; | |
| ['a', 'b', 'a', 'b']; | |
| ['foo', 'bar'].join(', '); | |
| y = 2; | |
| repeatString$('z', y); | |
| words = split$.call(text, ' '); | |
| unspaced = replace$.call(text, /\s+/, ''); | |
| ["a", "b", "c", "d", "e"].join('').replace('b', ''); | |
| function repeatString$(str, n){ | |
| for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str; | |
| return r; | |
| } |
This file contains hidden or 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
| # when the left one is an array literal | |
| ['a' 'b'] * 2 # array repetition | |
| # when the right one is a string literal | |
| <[ foo bar ]> * ', ' # array joining | |
| # or when the left one is a string | |
| y = 2 | |
| 'z' * y # string repetition | |
| words = text / ' ' # string division | |
| # or even when the right one is either a string or a regexp | |
| unspaced = text - /\s+/ | |
| [\a to \e] * '' - 'b' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment