What exactly did the three-point operator do?
const obj7 = {a: {b: 1}};
const obj8= { ...obj7 }
obj7.a.b = 7
console.log(obj7.a.b, obj8.a.b)
// 7 7
const obj9 = {a: 1};
const obj10= { ...obj9 }
obj10.a = 7
console.log(obj9.a, obj10.a)
// 1 7
my old friend doesn"t understand
Why is the second piece of code copied shallowly? And why is the first paragraph different from the second paragraph?
I seem to have missed something in the document, but I can"t find an explanation.
ask the boss to wake me up.
-split line-
I get it, but what does this three-point operator do? What"s the difference between and var =?
const obj3 = {a: 1};
var obj4 = obj3
obj3.a = 3;
console.log(obj3.a,obj4.a)
// 3 3
const obj9 = {a: 1};
const obj10= { ...obj9 }
obj10.a = 7
console.log(obj9.a, obj10.a)
// 1 7
whether it is. Or assign is a shallow copy, what is a shallow copy? as the name implies, only the first layer attribute is copied. If the value of the first layer attribute is data of a reference type, then the address of the data is copied, that is, the data itself is not copied over. It just means that I remember you, and I will call you later.
in the first example, the an attribute is an object. If you copy it and change the value of this data, it is equivalent to changing the original data
.
in the second example, the an attribute is a value of namber type, not a reference type, so copy it directly. After changing the value, the source object will not change.
< hr >
.
is a new operator for ES6 called deconstruction. For more information, please see teacher Ruan Yifeng's introduction to ECMAScript 6
, as the name implies, is to decompose the structure of a data, so what data can be decomposed?
all traverable data (objects with Iterator interfaces), such as objects, arrays, Map, Set, argument, other strings can also be structured (in a sense, strings can be understood as arrays).
what do you get when you structure a data?
this is about traversing the data. You can understand the structure as traversing values, but not all values are taken. Take object, for example, using for.in to traverse, you get all enumerable properties in common , while using structures to structure an object, you get all enumerable private properties , you should know.
There are two most commonly used places for
structures, one is structure assignment, the other is structure replication. The basic principle is traversal value .
http://es6.ruanyifeng.com/-sharpRE.