Nodejs Array ForEach forEach(fun /*, thisp*/)

Here you can find the source of forEach(fun /*, thisp*/)

Method Source Code

/*//from www.j a  va 2 s  .c  om
 * Copyright 2010 Jive Software
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Invokes `fun` on each element of the array in turn.
 *
 * The first argument given to `fun` is a single array element and the second
 * argument is the index of that element in the array.
 *
 * This definition is compatible with the JavaScript 1.6 definition for
 * `Array#forEach` in Spidermonkey.
 *
 * This implementation comes from:
 * https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
 *
 * @function
 * @param   {Function}  fun     function that will be applied to each array
 * element
 * @param   {Object}    [thisp] context in which `fun` will be invoked - `this`
 * in `fun` will refer to `thisp`
 */
Array.prototype.forEach = Array.prototype.forEach || function(fun /*, thisp*/) {
    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);
        }
    }
};

Related

  1. forEach(fn)
    Array.prototype.forEach=function(fn){
        var arr=this;
        var length=this.length;
        for(var i=0;i<length;i++){
            fn(arr[i],i,arr);
    
  2. forEach(fn)
    Array.prototype.forEach = function(fn)
        for (var i=0;i<this.length;i++)
            fn(this[i]);
    
  3. forEach(fn, context)
    Array.prototype.forEach = function(fn, context) {
      if (typeof fn != "function") {
        throw new TypeError(fn + " is not a function");
      if (typeof context === 'undefined') {
        context = this;
      for (var i = 0, l = this.length; i < l; ++i) {
        fn.call(context, this[i], i, this);
    ...
    
  4. forEach(fun /*, thisp */)
    Array.prototype.forEach = Array.prototype.forEach || function(fun ) {
      "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();
      var thisp = arguments[1];
      for (var i = 0; i < len; i++) {
        if (i in t) fun.call(thisp, t[i], i, t);
    ...
    
  5. 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)
    ...
    
  6. 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();
    ...
    
  7. forEach(func)
    Array.prototype.forEach = function(func)
      for(var i = 0, l = this.length; i < l; i ++)
        func(this[i]);
    };
    
  8. forEach(func)
    Array.prototype.forEach = function (func) {
        var length = this.length;
        for (var i = 0; i < length; i++) {
            func(this[i]);
    
  9. forEach(func, thisObj)
    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);
      };
    });