Skip to content

Instantly share code, notes, and snippets.

@ZhihaoLau
Created October 14, 2016 02:42
Show Gist options
  • Save ZhihaoLau/47f63f9306f325204bfd1f933d154a51 to your computer and use it in GitHub Desktop.
Save ZhihaoLau/47f63f9306f325204bfd1f933d154a51 to your computer and use it in GitHub Desktop.
‘Ruby times method’ in JavaScript
// Ruby = 5.times { |i| puts i }
// JS = (1).times(function(i){console.log(i);})
Number.prototype.times = function(cb) {
var i = -1;
while (++i < this) {
cb(i);
}
return +this;
}
@tanmayforgit
Copy link

what is the purpose of returning +this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment