您的位置:首页 > Web前端 > JavaScript

js对象深拷贝

2015-05-03 15:24 423 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
var obj = {
a: { b: 10 }
}

function copy(obj){
if(typeof obj != 'object'){
return obj;
}
var newObj = {};
for(attr in obj){
newObj[attr] = copy(obj[attr]);
}
return newObj;
}

var obj2 = copy(obj);
obj2.a.b =20;
alert(obj.a.b);//10
</script>
</head>
<body>

</body>
</html>


利用递归对复杂对象进行拷贝
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: