js深拷贝 发表于 2018-04-21 | 分类于 javascript | 阅读次数: js深拷贝1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859export const $ = function() { var copyIsArray, toString = Object.prototype.toString, hasOwn = Object.prototype.hasOwnProperty, class2type = { '[object Boolean]': 'boolean', '[object Number]': 'number', '[object String]': 'string', '[object Function]': 'function', '[object Array]': 'array', '[object Date]': 'date', '[object RegExp]': 'regExp', '[object Object]': 'object' }, type = function(obj) { return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"; }, isWindow = function(obj) { return obj && typeof obj === "object" && "setInterval" in obj; }, isArray = Array.isArray || function(obj) { return type(obj) === "array"; }, isPlainObject = function(obj) { if (!obj || type(obj) !== "object" || obj.nodeType || isWindow(obj)) { return false; } if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) { return false; } var key; for (key in obj) {} return key === undefined || hasOwn.call(obj, key); }, extend = function(deep, target, options) { for (var name in options) { var src = target[name]; var copy = options[name]; if (target === copy) { continue; } if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) { if (copyIsArray) { copyIsArray = false; var clone = src && isArray(src) ? src : []; } else { var clone = src && isPlainObject(src) ? src : {}; } target[name] = extend(deep, clone, copy); } else if (copy !== undefined) { target[name] = copy; } } return target; }; return { extend: extend };}();