Apply the function {handler} to each item in the object, ignoring inherited items. - Node.js Object

Node.js examples for Object:Inheritance

Description

Apply the function {handler} to each item in the object, ignoring inherited items.

Demo Code


/**/*from  w w w.j a  v a  2s  .c om*/
 * Apply the function {handler} to each item in the object, ignoring inherited items.
 * Return false in the {handler} to break the cycle.
 * @param {Function} handler
 * @version 1.0.0
 * @date August 20, 2010
 * @since August 20, 2010
 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
 * @license MIT License {@link http://creativecommons.org/licenses/MIT/}
 */
Object.prototype.each = function(handler){
  // Check
  if ( typeof handler !== 'function' ) {
    throw new Exception('Object.prototype.each: Invalid input');
  }
  // Cycle
  for ( var key in this ) {
    // Check
    if ( !this.hasOwnProperty(key) ) {
      continue;
    }
    // Fire
    var value = this[key];
    if ( handler.apply(value,[key,value]) === false ) {
      break;
    }
  }
  // Chain
  return this;
};

Related Tutorials