All files / obj assign.js

100% Statements 8/8
83.33% Branches 5/6
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29                            222x 222x   223x 223x 133x 133x       222x     10x  
/**
 * 扩展并覆盖对象
 * @method obj/assign
 * @param {Object} obj 要扩展的对象
 * @param {Object} item 要扩展的属性键值对
 * @returns {Object} 扩展后的源对象
 * @example
 * var $assign = require('@spore-ui/kit/packages/obj/assign');
 * var obj = {a: 1, b: 2};
 * console.info($assign(obj, {b: 3, c: 4})); // {a: 1, b: 3, c: 4}
 * console.info($assign({}, obj, {b: 3, c: 4})); // {a: 1, b: 3, c: 4}
 */
 
function assign(obj) {
  obj = obj || {};
  Array.prototype.slice.call(arguments, 1).forEach(function (source) {
    var prop;
    source = source || {};
    for (prop in source) {
      Eif (source.hasOwnProperty(prop)) {
        obj[prop] = source[prop];
      }
    }
  });
  return obj;
}
 
module.exports = assign;