Posted By

peta on 10/30/09


Tagged

javascript js hack number python prototype loop repeat shortcut ror


Versions (?)

JS: Savvy looping on a Number


 / Published in: JavaScript
 

The Number objects value determines how often the supplied function is repeated. Inspired by ROR & Python. "Pollutes" Number's prototype.

  1. Number.prototype.times = function(fn) {
  2. var i = (this > 0) ? this : 0;
  3. while (i--) {
  4. fn();
  5. }
  6. }

Report this snippet  

You need to login to post a comment.