Javascript Array every(fn, context)

Description

Javascript Array every(fn, context)


/**//from  www.  j  a va2s  .  c om
 * @param {function} fn(value, i, array)
 * @param {object} [context]
 * @return {boolean} - fn() ~= true then return true; fn() ~= false then break and return false
 */
Array.prototype.every = Array.prototype.every || function(fn, context){
  for(var i = 0; i < this.length; i++){
    if(!fn.call(context, this[i], i, this)){
      return false;
    }
  }
  return true;
};

Javascript Array every(fn, context)

Array.prototype.every = function(fn, context) {
  if (typeof fn != "function") {
    throw new TypeError(fn + " is not a function");
  }/*w ww.  j av a2 s.  c  o m*/

  if (typeof context === 'undefined') {
    context = this;
  }

  for (var i = 0, l = this.length; i < l; i++) {
    if (this.hasOwnProperty(i) && !fn.call(context, this[i], i, this)) {
      return false;
    }
  }

  return true;
}



PreviousNext

Related