JS: Savvy looping on a Number


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. Number.prototype.times = function(fn) {
  2. var i = (this > 0) ? this : 0;
  3. while (i--) {
  4. fn();
  5. }
  6. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.