Home >Web Front-end >JS Tutorial >Implement deep copy and shallow copy in jquery
var toString = [].toString, hasOwn = Object.prototype.hasOwnProperty;var types = { '[object Boolean]':'bool', '[object Number]' : 'number', '[object String]' : 'string', '[object Object]':'object', '[object Array]':'array', '[object Function]':'function', '[object Date]' : 'date', '[object RegExp]' : 'regExp', };var type = function(obj){ return obj === null ? String(obj) : types[toString.call(obj)] || 'object'; };var isWindow = function(obj){ return obj && typeof obj === 'object' && 'setInterval' in obj; };var isArray = Array.isArray || function(obj){ return type(obj) === 'array'; };var isPlainObject = function(obj){ if(!obj || type(obj) !== 'object' || isWindow(obj) || obj.nodeType){ return false; } try{ if(obj.constructor && !hasOwn.call(obj, 'constructor') && !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf')){ return false; } }catch(e){ return false } var key; for(key in obj){} return key === undefined || hasOwn.call(obj, key) }function extend(deep, target, ref){ var name, value, src, copy, clone, copyIsArray; for(name in ref){ src = target[name]; copy = ref[name]; if(target === copy){ continue; } if(deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))){ if(copyIsArray){ clone = src && isArray(src) ? src : []; }else{ clone = src && isPlainObject(src) ? src : {}; } target[name] = extend(deep,clone,copy); }else{ target[name] = copy; } } return target; }function extend2(target, ref){ var name, value; for(name in ref){ value = ref[name]; if(value !== undefined){ target[name] = value; } } return target; }
The above is the detailed content of Implement deep copy and shallow copy in jquery. For more information, please follow other related articles on the PHP Chinese website!