Nodejs Function Call times(f, context)

Here you can find the source of times(f, context)

Method Source Code

/**//from   w w w .j av a 2 s  .  c o  m
 * Created by yuanli on 9/18/2016.
 */
function Range(from, to) {
    this.from = from;
    this.to = to;
};

Range.prototype = {
    constructor: Range,
    includes: function(x) {
        return this.from <= x && x <= this.to;
    },
    foreach: function(f) {
        for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
    },
    toString: function() {
        return "(" + this.from + "..." + this.to + ")";
    }
};

var r = new Range(1,3);
r.includes(2);
r.foreach(console.log);
console.log(r);



Number.prototype.times = function(f, context){
    var n = Number(this);
    for(var i = 0; i < n; i++) {
        f.call(context, i);
    }
};

var n = 3;
n.times(function(n) {
    console.log(n + "Hello")
});

Related

  1. times(n)
    Function.prototype.times = function (n) {
      if (n <= 0) {
        throw new TypeError('Invalid argument');
      var fn = this;
      return function () {
        for (var i = 0; i < n; i += 1) {
          fn.call(null, arguments);
      };
    };
    
  2. partial()
    Function.prototype.partial = function(){
        var fn = this, args = Array.prototype.slice.call(arguments);
        return function(){
          var arg = 0;
          for ( var i = 0; i < args.length && arg < arguments.length; i++ )
            if ( args[i] === undefined )
              args[i] = arguments[arg++];
          return fn.apply(this, args);
        };
    ...
    
  3. map(map)
    Function.prototype.map = function (map) {
        var params = this.getParamNames();
        var fn = this;
        for (var i in params)
            fn = fn.bind({}, map[params[i]]);
        return fn;
    };
    
  4. times(f, context)
    Number.prototype.times = function(f, context){
      var n = Number(this);
      for(var i = 0; i < n; i++){
        f.call(context, i);
    };
    
  5. times(f, context)
    Number.prototype.times = function(f, context) {
      var n = Number(this);
      for(var i = 0; i < n; i++) f.call(context, i);
    };
    String.prototype.trim = String.prototype.trim || function() {
      if (!this) return this; 
      return this.replace(/^\s+|\s+$/g, ""); 
    };
    Function.prototype.getName = function() {
    ...
    
  6. times(fn)
    'use strict';
    Number.prototype.times = function(fn) {
      if (this.valueOf()) {
        for (var i = 0; i < this.valueOf(); i++) {
          fn(i);
    };
    function repeat(n, fn) {
    ...
    
  7. times(func)
    var i = 0;
    Number.prototype.times = function(func) {
      let n = Math.round(this);  
      if (n <= 0) return null;
      func();
      return (n - 1).times(func);
    console.log(i);
    
  8. times(func)
    Number.prototype.times = function (func) {
      var i;
      for (i = 0; i < this; i += 1) {
        func();
    };
    function random(min, max) {
        var floor = +min,
            ceil = +max;
    ...