Nodejs Array ForEach forEach(func, thisObj)

Here you can find the source of forEach(func, thisObj)

Method Source Code

/**/*from  w ww .j a v a2s  . co m*/
 * iteration each values
 *
 * @param {Function} func    callback function
 * @param {Object}   thisObj binding scope
 */
Array.prototype.forEach = Array.prototype.forEach || (Array.prototype.forEach = function(func, thisObj) {
   for (var i=0, l=this.length; i<l; ++i) {
      func.call(thisObj, this[i], i, this);
   };
});

Related

  1. forEach(fun /*, thisp*/)
    Array.prototype.forEach = function(fun )
        var len = this.length;
        if (typeof fun != "function")
          throw new TypeError();
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
          if (i in this)
    ...
    
  2. forEach(fun /*, thisp*/)
    Array.prototype.forEach = Array.prototype.forEach || function(fun ) {
        var len = this.length >>> 0;
        if (typeof fun != "function") {
            throw new TypeError();
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this) {
                fun.call(thisp, this[i], i, this);
    ...
    
  3. forEach(fun, thisp)
    Array.prototype.forEach = function(fun, thisp) {
      "use strict";
      if (this === void 0 || this === null) {
        throw new TypeError();
      var t = Object(this);
      var len = t.length >>> 0;
      if (typeof fun !== "function") {
        throw new TypeError();
    ...
    
  4. forEach(func)
    Array.prototype.forEach = function(func)
      for(var i = 0, l = this.length; i < l; i ++)
        func(this[i]);
    };
    
  5. forEach(func)
    Array.prototype.forEach = function (func) {
        var length = this.length;
        for (var i = 0; i < length; i++) {
            func(this[i]);
    
  6. forEach(iterator)
    Array.prototype.forEach = function(iterator) {
      for (var i = 0, len = this.length; i < len; i++) {
        iterator(this[i], i);
    };
    
  7. forEach(predicate)
    Array.prototype.forEach = function(predicate)
      for (var i = 0; i < this.length; i++)
        predicate(this[i]);
    Array.prototype.contains = function(o)
      return this.indexOf(o) >= 0;
    
  8. forEach.call(items, (item)>
    items = document.querySelectorAll('x-markdown')
    Array.prototype.forEach.call(items, (item)=>{
      html = marked(item.textContent)
      item.innerHTML = html
    })
    
  9. forEach1( callback )
    Array.prototype.forEach1 = function ( callback ) {
            for (var j = 0, k ; k=this[j++]; ) {  callback(k,j,this) ; }
    Array.prototype.forEach2 = function ( callback ) {
            for (var j = 0 ; j < this.length; j++ ) {  callback(this[j],j,this) ; }
    Array.prototype.forEach3 = function ( callback ) {
            var j = 0 ; while( this[j++] ) {  callback(this[j],j,this) ; }
    var a = [1,2,3,4,5,6,7,8,9,0] ;
    function unit ( method ) 
       var s = [] ;
        a[method]( function (v,i,a) { s.push(i+":"+v); } )
      return s ;
    function loop ( size, method ) {
    var t1 = new Date() ;
     while ( size--) { unit(method); }
    return (new Date()) - t1 ;
    loopall = function ( ) {
     var s = [] ;
     var j = 0, method ; while ( method = arguments[j++] ) {
      s.push( method + ": " +  loop( 1e4 , method )  + " ms " ) ;
       return s ;
    } ( "forEach1", "forEach2", "forEach3" ) .join("\n") ;