HTML/Html-Javascript/14-解构,对象合并.html

44 lines
843 B
HTML
Raw Normal View History

2023-05-01 19:37:40 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//解构
let data = { key1:"value1",key2:"value2",key3:"value3"};
//常规
// let key1=data.key1;
//结果与上无异
// let { key1: 新名字 } = data;
// console.log(key1);
// console.log(新名字);
//合并
let o0= {name:'0',age:16,m:"好"};
let o1={name:'1',m:"额",f:"哭"};
// 将上述两个对象,合并成一个对象(o2),且当key 冲突时,以o1为主
let o2=Object.assign(o0,o1);//可以无数个,且最后面的优先级高
let o3 ={...o0,...o1};
console.log(o2);
</script>
</body>
</html>