12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- var arrayEach = require('./arrayEach')
- var keys = require('./keys')
- var isArray = require('./isArray')
- var clone = require('./clone')
- var objectAssignFns = Object.assign
- function handleAssign (destination, args, isClone) {
- var len = args.length
- for (var source, index = 1; index < len; index++) {
- source = args[index]
- arrayEach(keys(args[index]), isClone ? function (key) {
- destination[key] = clone(source[key], isClone)
- } : function (key) {
- destination[key] = source[key]
- })
- }
- return destination
- }
- /**
- * 将一个或多个源对象复制到目标对象中
- *
- * @param {Object} target 目标对象
- * @param {...Object}
- * @return {Boolean}
- */
- var assign = function (target) {
- if (target) {
- var args = arguments
- if (target === true) {
- if (args.length > 1) {
- target = isArray(target[1]) ? [] : {}
- return handleAssign(target, args, true)
- }
- } else {
- return objectAssignFns ? objectAssignFns.apply(Object, args) : handleAssign(target, args)
- }
- }
- return target
- }
- module.exports = assign
|