es6 new operators include: 1. Optional chain operator "?.", which can determine whether the properties before the operator are valid, thereby reading the properties of the object in a chain or returning undefined; 2. Exponential operation Operator "**", infix operator used for exponential operations, syntax "x ** y"; 3. Null value merging operator "??"; 4. Logical assignment operators "&&=", "|| =", "??=", mainly perform logical operations on itself, and then assign the subsequent value to it; 5. Extension operator "...".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Interviewer: Can you tell us about the new operators in es6?
Interviewer: Um. . .
Interviewer: For example, optional chaining operator?
Interviewer: Um. . .
Interviewer: Okay, that’s it for today’s interview.
Interviewer: Um. . .
The above scenes are purely fictitious and may be similar, hahaha?.
Today, let’s learn several new operators in es6 and consolidate them.
A property of an object. If this property is an object, it also There are sub-properties. When accessing this sub-property, such as
var obj = {a: {key: 'val'}} console.log(obj.a.key) // val
, if the property does not exist, an error may be reported.
var obj = {key: 'val'} console.log(obj.a.key) // error: Cannot read properties of undefined (reading 'key')
If you want to be compatible with this situation, you need to add a default value to be compatible
var obj = {key: 'val'} console.log((obj.a || {}).key) // undefined
If the level is too deep, it will be difficult to read.
(((obj.a || {}).b || {}).c || {}).d // 或者 obj.a && obj.a.b && obj.a.b.c && obj.a.b.c.d
The optional chain operator is used to improve this writing method. It is represented by ?.
.
The above example can be rewritten using the optional chain operator
obj.a?.key obj.a?.b?.c?.d
The effect is the same. Does it increase readability and save code?
If it is found that there is no such attribute, subsequent point operations will not be performed.
It can also be written when the function is executed. Compatible with cases where the function may not be a function. In this case, the function name needs to be defined first or have a value, otherwise an error will still be reported.
var fn fn?.() // 不会报错 fn1?.() // 报错
Optional chaining cannot be used on the super keyword, template strings, instantiation constructors, the left side of assignment operators, etc.
super?.fn() // error new Fn?.a() // error obj.a?.`${b}` // error obj?.a = obj
We used to calculate powers like this
Math.pow(2,3) // 8
Now we can calculate it through the exponent operator, use**
means
2 ** 3 // 8
can also be written consecutively
2** 3 ** 3 // 134217728
. Maybe you are wondering why it is so big, because it is calculated from the right. Equivalent to 2**(3 ** 3)
.
This operator is represented by ??
. The default value will only be executed when the value on the left is undefined
or null
.
Let’s take a look at an example:
var a = '' ?? 'default' console.log(a) // '' var a = 0 ?? 'default' console.log(a) // 0 var a = 123 ?? 'default' console.log(a) // 123 var a = undefined ?? 'default' console.log(a) // default var a = undefined ?? 'default' console.log(a) // default
If mixed with &&
or ||
, you need to add ()
Display the priority, otherwise an error will be reported.
var a = undefined ?? 'default' && 'a' // error
There are three logical assignment operators:
Mainly performs logical operations on itself, and then Assign the following value to it.
Let’s take a look at it through an example:
var a,b,c a &&= 1 // undefined //等同于 a && (a = 1) b ||= 1 // 1 //等同于 b || (b = 1) c ??= 1 // 1 //等同于 c ?? (c = 1)
Expand operator … was introduced in ES6 to expand the iterable object into its separate elements , the so-called iterable object is any object that can be traversed using a for of loop, such as: array, string, Map, Set, DOM node, etc.
1. Copy array objects
Using the expansion operator to copy an array is a common operation in ES6:
const years = [2018, 2019, 2020, 2021]; const copyYears = [...years]; console.log(copyYears); // [ 2018, 2019, 2020, 2021 ]
The expansion operator copies an array, onlyThe first layer is deep copy, that is, using the spread operator to copy a one-dimensional array is a deep copy. See the following code:
const miniCalendar = [2021, [1, 2, 3, 4, 5, 6, 7], 1]; const copyArray = [...miniCalendar]; console.log(copyArray); // [ 2021, [ 1, 2, 3, 4, 5, 6, 7 ], 1 ] copyArray[1][0] = 0; copyArray[1].push(8); copyArray[2] = 2; console.log(copyArray); // [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 2 ] console.log(miniCalendar); // [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ]
Put the printed results together to make it clearer. The comparison is as follows:
Variable description | Result | Operation |
---|---|---|
copyArray | [ 2021, [ 1, 2, 3, 4, 5, 6, 7 ], 1 ] | Copy ArrayminiCalendar |
##copyArray
| [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 2 ]
| 1. Reassign the first element of the second element of the array to 0; 2. Add an element 8 to the second element of the array; 3. Reassign the third element of the array to 2|
miniCalendar
| [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ]
| From the results, the second element of the array is an array, which is larger than 1 dimension. Changes to the elements inside will cause the original variable to The value changes accordingly