console
var person = {
name: '响水滩',
hobby: ['吃饭', ['睡觉', '踢足球']],
date: new RegExp('\\w+'),
function () {}
}
var person1 = JSON.parse(JSON.stringify(person))
person1.name = '乌蒙山'
person1.hobby[0] = '打游戏'
console.log(person)
console.log(person1)
function shallowCopy (obj) {
var target = {}
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
target[i] = obj[i]
}
}
return target
}
function deepClone (obj) {
var target = new obj.constructor()
if (typeof obj !== 'object') return obj
if (obj instanceof RegExp) return new RegExp(obj)
if (obj instanceof Date) return new Date(obj)
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
target[i] = deepClone(obj[i])
}
}
return target
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=, initial-scale=">
<meta http-equiv="X-UA-Compatible" content="">
<title>JS的拷贝</title>
</head>
<body>
</body>
</html>