ES6 assign
程序员文章站
2022-03-21 22:13:16
...
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>ES6 assign</title>
<style type="text/css">
html {height: 100%;}
body {width: 200px;height: 100%;position: relative;padding: 0;margin: 0;}
</style>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/1.9.0/jquery.js"></script>
<script>
var name = "zhangsan";
var password = "1111111";
var obj = {
name,
password,
arr: [1, 2, 3, 4],
sayName() {
console.log(this.name);
}
};
var obj1 = {
age: 20,
name: 'xutongbao'
};
//花括号叫目标对象,后面的obj、obj1是源对象。对象合并是指:将源对象里面的属性添加到目标对象中去,若两者的属性名有冲突,后面的将会覆盖前面的
var nObj = Object.assign({}, obj, obj1);
console.log(nObj); //{name: "xutongbao", password: "1111111", arr: Array(4), sayName: ƒ, age: 20}
nObj.arr.push(5);
console.log(obj.arr); //[1, 2, 3, 4, 5] 此处说明assign是浅拷贝
var arr11 = Object.assign([1,2,3], [4,5]);
console.log(arr11); //[4,5,3] 对象是根据属性名来对应,数组是根据索引号来对应
</script>
</body>
</html>
下一篇: 来自小白的第三次C语言打卡