Extends the current object with the passed object(s), ignoring inherited properties. - Node.js Object

Node.js examples for Object:Inheritance

Description

Extends the current object with the passed object(s), ignoring inherited properties.

Demo Code


/**/*from w w w  .ja v  a  2 s . c o m*/
 * Extends the current object with the passed object(s), ignoring inherited properties.
 * @param {Object} ... The passed object(s) to extend the current object with
 * @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.extend = function(object){
  var Me = this;
  // Check
  if ( typeof object !== 'object' ) {
    throw new Exception('Object.prototype.extend: Invalid input');
  }
  // Handle
  if ( arguments.length > 1 ) {
    arguments.each(function(){
      Me.extend(this);
    });
  }
  else {
    // Extend
    object.each(function(key,object){
      if ( typeof object === 'object' ) {
        if ( object instanceof Array ) {
          Me[key] = [].extend(object);
        }
        else if ( typeof Me[key] === 'object' ) {
          var backup = Me[key];
          Me[key] = {}.extend(backup).extend(object);
        }
        else {
          Me[key] = {}.extend(object);
        }
      }
      else {
        Me[key] = object;
      }
    });
  }
  // Chain
  return this;
};

Related Tutorials