Created
July 26, 2019 11:32
-
-
Save Fujihai/be49608a3b1e740d94e0d6eaab0ca15f to your computer and use it in GitHub Desktop.
JavaScript 函数对象深拷贝 // source https://jsbin.com/yuxefuc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JavaScript 函数对象深拷贝</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
//对要拷贝的元素进行类型判断 | |
function getType(obj) { | |
var toString = Object.prototype.toString; | |
var map = { | |
'[object Boolean]' : 'boolean', | |
'[object Number]' : 'number', | |
'[object String]' : 'string', | |
'[object Function]' : 'function', | |
'[object Array]' : 'array', | |
'[object Date]' : 'date', | |
'[object RegExp]' : 'regExp', | |
'[object Null]' : 'null', | |
'[object Object]' : 'object', | |
}; | |
if(obj instanceof Element) { | |
return 'element'; | |
} | |
return map[toString.call(obj)]; | |
} | |
function deepClone(data) { | |
var type = getType(data); | |
var obj; | |
if(type === 'array') { | |
obj = []; | |
}else if(type === 'object') { | |
obj = {}; | |
}else { | |
return data; | |
} | |
if(type === 'array') { | |
for(var i = 0, len = data.length; i < len; i++) { | |
obj.push(deepClone(data[i])); | |
} | |
}else if(type === 'object') { | |
for(var key in data) { | |
obj[key] = deepClone(data[key]); | |
} | |
} | |
return obj; | |
} | |
var obj1 = {}; | |
var obj2 = { | |
a: 1, | |
b: 2, | |
c: { | |
d: 3, | |
e: { | |
f: 4, | |
g: 5 | |
} | |
} | |
}; | |
obj1 = deepClone(obj2); | |
console.log(obj1); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">//对要拷贝的元素进行类型判断 | |
function getType(obj) { | |
var toString = Object.prototype.toString; | |
var map = { | |
'[object Boolean]' : 'boolean', | |
'[object Number]' : 'number', | |
'[object String]' : 'string', | |
'[object Function]' : 'function', | |
'[object Array]' : 'array', | |
'[object Date]' : 'date', | |
'[object RegExp]' : 'regExp', | |
'[object Null]' : 'null', | |
'[object Object]' : 'object', | |
}; | |
if(obj instanceof Element) { | |
return 'element'; | |
} | |
return map[toString.call(obj)]; | |
} | |
function deepClone(data) { | |
var type = getType(data); | |
var obj; | |
if(type === 'array') { | |
obj = []; | |
}else if(type === 'object') { | |
obj = {}; | |
}else { | |
return data; | |
} | |
if(type === 'array') { | |
for(var i = 0, len = data.length; i < len; i++) { | |
obj.push(deepClone(data[i])); | |
} | |
}else if(type === 'object') { | |
for(var key in data) { | |
obj[key] = deepClone(data[key]); | |
} | |
} | |
return obj; | |
} | |
var obj1 = {}; | |
var obj2 = { | |
a: 1, | |
b: 2, | |
c: { | |
d: 3, | |
e: { | |
f: 4, | |
g: 5 | |
} | |
} | |
}; | |
obj1 = deepClone(obj2); | |
console.log(obj1); | |
}</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//对要拷贝的元素进行类型判断 | |
function getType(obj) { | |
var toString = Object.prototype.toString; | |
var map = { | |
'[object Boolean]' : 'boolean', | |
'[object Number]' : 'number', | |
'[object String]' : 'string', | |
'[object Function]' : 'function', | |
'[object Array]' : 'array', | |
'[object Date]' : 'date', | |
'[object RegExp]' : 'regExp', | |
'[object Null]' : 'null', | |
'[object Object]' : 'object', | |
}; | |
if(obj instanceof Element) { | |
return 'element'; | |
} | |
return map[toString.call(obj)]; | |
} | |
function deepClone(data) { | |
var type = getType(data); | |
var obj; | |
if(type === 'array') { | |
obj = []; | |
}else if(type === 'object') { | |
obj = {}; | |
}else { | |
return data; | |
} | |
if(type === 'array') { | |
for(var i = 0, len = data.length; i < len; i++) { | |
obj.push(deepClone(data[i])); | |
} | |
}else if(type === 'object') { | |
for(var key in data) { | |
obj[key] = deepClone(data[key]); | |
} | |
} | |
return obj; | |
} | |
var obj1 = {}; | |
var obj2 = { | |
a: 1, | |
b: 2, | |
c: { | |
d: 3, | |
e: { | |
f: 4, | |
g: 5 | |
} | |
} | |
}; | |
obj1 = deepClone(obj2); | |
console.log(obj1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment