Skillfully learn skillfully with 1. New Set ()
It may be known that the new data structure Set is provided in ES6, but there may not be many people who can use it flexibly. With the set data structure, we can easily re-use an array, for example:
Let arr = [1, 2, 2, 3];let set = new set (arr); let NEWARR = Array.from (set); The Array.from method can convert the Set structure to an array. Console.log (NEWARR); [1, 2, 3]
2. Object.assign ()
Object.assign () is also an extension of the object provided in ES6, but it can only copy one layer, such as:
Let obj1 = {A:1};let Obj2 = {B:2};let Obj3 = Object.assign ({}, obj1, obj2); Console.log (OBJ3); {a:1, b:2}
3. Map ()
The map method is used to iterate over an array, with a return value, to operate on each item of the array and to generate a new one, sometimes in place of a for and foreach loop, simplifying the code, such as:
Let ARR3 = [1, 2, 3, 4, 5];let newArr3 = Arr3.map ((e, i) + E * 10); Multiply each item of the array by 10console.log (NEWARR3); [10, 20, 30, 40, 50]
4. Filter ()
The filter method is also used to iterate through an array, as the name implies, to filter an array, trigger a callback function after each element, and, by judging, preserving or removing the current item, and finally returning a new array, such as:
Let ARR4 = [1, 2, 3, 4, 5];let newArr4 = Arr4.filter ((e, i) + e% 2 = = 0); The number of Console.log (NEWARR4) which is not 0 of the remainder is filtered; [2,4]
5. Some ()
The some method is used to iterate through an array, triggering a callback function after each element, returning true if one satisfies the condition, otherwise returning false, similar to | | Comparisons, such as:
Let Arr5 = [{result:true}, {result:false}];let NEWARR5 = Arr5.some ((e, i) = = E.result); As long as one is true, that is Trueconsole.log (NEWARR5); True
6.every ()
The Every method is used to iterate through an array, triggering a callback function after each element, and returning false if a condition is not met, otherwise true, similar to the && comparison, such as:
Let arr6 = [{result:true}, {result:false}];let newArr6 = Arr6.every ((e, i) = = E.result); As long as one is false, that is Falseconsole.log (NEWARR6); False
7. ~ ~ operator
~ Symbol used in JavaScript has a bitwise negation, ~ ~ is to take the inverse two times, and the operation value of the bitwise operation is an integer, the result is an integer, so the bit operation will automatically become an integer, can be cleverly removed fractional part, similar to parseint, such as:
Let A = 1.23;let B = -1.23;console.log (~~a); 1console.log (~~B); -1
8. | | Operator
Clever Use of | | Operators we can set default values for variables, such as:
Let c = 1;let D = C | | 2; If the value of C is true then the existing value is taken, otherwise 2console.log (d); 1
9 ..... Operator
... Operators are methods used in ES6 to deconstruct arrays, which can be used to quickly get the parameters of an array, such as:
Let [Num1, ... nums] = [1, 2, 3];console.log (NUM1); 1console.log (Nums); [2, 3]
10. Ternary operators
This operator should be familiar to everyone, in the case of dictation can simplify the wording of if else, such as:
Let E = true, F = ";" if (e) {f = ' man ';} else {f = ' woman ';} Equal to E? f = ' man ': f = ' woman ';
Conclusion
This article only lists the more common JavaScript syntax in the 10-point lifting coding efficiency of the method is simple to explain, of course, each knowledge point can be carried out corresponding to explore, I hope we learn skillfully at the same time to achieve the effect of skillfully.
JavaScript skillfully learns to use skillfully