<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
// var a={"name":"hello"};
// console.log(typeof(a));
//结论:b引用a的内容,改变b也会影响a
// var b=a;
// b.name='张三';
// console.log(b);
// console.log(a);
//1.浅度拷贝
var a={"name4":"hello"};
var b={};
for(var attr in a){
b.attr=a.attr;
}
b.name='张三';
console.log(b.name);
console.log(a.name);
//2.深度拷贝
var c={"name":{"age":"50"}};
var str=JSON.stringify(c);//把json转成字符串给str变量
var d=JSON.parse(str);//把字符串转成js
d.name.age="60";
console.log(c.name.age);
console.log(d.name.age);
//数组的拷贝
var aa=[1,2,3];
var bb=[];
for(var i=0;i<aa.length;i++){
bb.push(aa[i]);
}
</script>
</head>
<body>
</body>
</html>
深度拷贝、浅度拷贝、数组的拷贝
最新推荐文章于 2024-08-16 17:36:42 发布