浅拷贝

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
// 对象的拷贝
var obj1 = {
name: 'zs',
age: 18,
sex: '男',
dog: {
name: '金毛',
age: 2,
yellow: '黄色'
}
}

var obj2 = {};

// 封装函数 - 把o1 的成员,复制给o2

// 浅拷贝
function copy(o1, o2) {
for (var key in o1) {
o2[key] = o1[key];
}
}
copy(obj1, obj2);

// 修改obj1中的成员
obj1.name = 'xxxx';
obj1.dog.name = '大黄';

console.dir(obj2);

</script>
</body>
</html>

posted @ 2020-04-08 18:27  花木兰r  阅读(88)  评论(0编辑  收藏  举报