Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Write in front: the author will mainly focus on part of the key interview questions, this article is not as a rigorous introduction to ES6 document, only for you want to find a job to recall, strengthen memory part of the content, any questions please leave a message pointed out, the above. 🙂
let & const
- Variables defined by let & const can only be used after they are defined. Variables defined by var can be promoted
- Neither let nor const can be defined repeatedly
- A constant defined by const is immutable
Note the difference between base types and reference types
Destruct assignment of variables
- ES6 supports incomplete deconstruction, but the target object must be traversable
Const [a,b] = [1,2,3]; // a = 1 // b = 2 const [a,b,c] = [1]; // a = 1 // b = undefined // c = undefined // swap const [a,b] = [b,a];Copy the code
String new method
includes
: determines whether the string contains a string, and the second argument is to start the searchindex
Obviously, includes includes startsWith&endWith
repeat
: returns the result of a new string repeated many times
Note that the repeat takes one argument, the number of repetitions, and if the argument is not a positive integer, the function attempts to convert down to a positive integer (converted to 0 when the value is between (-1, 0)). Additionally, a RangeError is returned when the argument is < -1 or Infinity.
replaceAll
: It is now available in ES6replaceAll
.
New array method
Extended operator
: The concern here is
const arr = [1, 2, 3];
const arr2 = [...arr] // arr2 [1,2,3]
arr === arr2 // false
Copy the code
Note that extension operators can only be placed in parentheses when a function is called, otherwise an error will be reported.
Array.of()
: To make up forArray
The problem with constructors, i.eArray(1)
和Array (1, 2)
Totally different. Also, if there are no arguments, an empty array is returned.Array.fill()
: Used to populate an array.
Note that if the type of the population is an object, then the object is assigned to the same memory address, not the deep-copy object.
Object extension
- Object property traversal mode
ES6 has five methods for traversing an object’s properties.
- for… in
for… The in loop iterates over the object’s own and inherited enumerable properties (excluding the Symbol property).
- Object.keys(obj)
Keys returns an array containing the key names of all of the Object’s own (not inherited) enumerable properties (not including the Symbol property).
- Object.getOwnPropertyNames(obj)
Object. GetOwnPropertyNames returns an array containing all attributes of the Object itself (excluding Symbol attribute, but cannot be enumerated attribute) of keys.
- Object.getOwnPropertySymbols(obj)
Object. GetOwnPropertySymbols returns an array containing all the Symbol attribute of the Object itself the key name.
- Reflect.ownKeys(obj)
Reflect.ownKeys returns an array containing all of the object’s own (not inherited) key names, whether they are symbols or strings or enumerable.
All five methods follow the same sequence rules for traversal of attributes.
- First, all numeric keys are iterated, sorted in ascending order.
- Next, all the string keys are iterated in ascending order by the time they were added.
- Let’s go through everything one last time
Symbol
Keys, in ascending order of accession time.
Reflect.ownKeys({
[Symbol()]:100,
b:100,
3:100,
100: 100,
2:100,
a:100
})
// ['2','3','100','b','a',Symbol()]
Copy the code
In the above code, the reflect. ownKeys method returns an array containing all the properties of the argument object. The array’s properties are in this order, starting with the numeric properties 2 and 10, followed by the string properties B and A, and finally the Symbol property.
Object adding method
- Object.is()
Object.is() is used to compare whether two values are exactly equal. Unlike ===, +0 does not equal -0 and NaN equals itself.
- Object.assign()
The object.assign () method is used to merge objects, copying all of the source Object’s own and enumerable attributes to the target Object.
We often use object. assign as a shallow copy of an Object — although strictly speaking this approach doesn’t make sense, since it misses possible non-enumerable attributes.
let obj = { a: 1 }
obj === Object.assign(obj) // true
Copy the code
The operator
a**b
= = =Math.pow(a,b)
a? .b
= = =(a === null || a === undefined) ? undefined : a.b
a ?? b
= = =a === null ? b : a
If multiple logical operators are used together, parentheses must indicate precedence or an error will be reported.