Javascript Array some(fn, context)

Description

Javascript Array some(fn, context)


Array.prototype.some = function(fn , context) {
  if (typeof fn != "function") {
    throw new TypeError(fn + " is not a function");
  }//from   w  w w.  j a  v  a  2  s  . co  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 true;
    }
  }

  return false;
}

Javascript Array some(fn, context)


/**/*from   w  w w  .  ja  va 2 s. c  o m*/
 * @param {function} fn(value, i, array)
 * @param {object} [context]
 * @return {boolean} - fn() ~= true then break and return true; fn() ~= false then return false
 */
Array.prototype.some = Array.prototype.some || function(fn, context){
  for(var i = 0; i < this.length; i++){
    if(fn.call(context, this[i], i, this)){
      return true;
    }
  }
  return false;
};



PreviousNext

Related