egoist
8/10/2015 - 2:53 PM

Operator Overloading

Operator Overloading

# 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'
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;
}