ES6 Other articles:
ES6 learning let&const juejin.cn/post/684490…
. Operator definition
. The operator is also called the expansion collection operator. . Operators have different roles in different places. 1. Write ———— collection function
function test(... Arg) {} the test (1, 2, 3)Copy the code
2. Read ———— to expand the function
Var arg = [1,2,3] console.log(... arg) //1 2 3Copy the code
. Operator action
1. Simplify writing length 2. Improve development efficiency
Write expansion
Undefined arguments can be accepted, using arguments and… The sum of the indeterminate arguments by the operator.
Function sum() {// Arguments let sumNumber = 0 for(let I = 0; i < arguments.length; I ++) {sumNumber += arguments[I]} console.log(sumNumber) // 15 return sumNumber} sum(1,2,3,4,5) function sum(... Arg) {// Use... Operator console.log(arg) //[1, 2, 3, 4, 5] arg is an array, ForEach (function(ele) {sumNumber += ele}) console.log(sumNumber) // 15 return SumNumber} sum (1, 2, 3, 4, 5)Copy the code
It can be seen that using… Operators are more concise.
. Operators can be preceded by fixed parameters, and values can be passed in. But… Operators are not allowed to pass in parameters.
function test(a,b,... Arg) {the console. The log (a, b, arg) / / 10 20 [" aa ", "bb"]} the test (10, 20, 'aa', 'bb')Copy the code
The following code uses… The operator writes an average value minus the highest value and minus the lowest value
function average(... arg) { arg.sort(function(a,b) { return a - b }) arg.pop() arg.shift() console.log(arg) //[2, 3, 4, 5, 6] return computedScore(... arg) } function computedScore(... arg) { let sum = 0 arg.forEach(function (ele) { sum += ele }) console.log(sum/arg.length) //4 return sum/arg.length } Business (1,2,3,4,5,6,7)Copy the code
It can be seen that using… Operators are obviously more convenient than arguments.
Reading expansion
. [1,2,3,4,5] is equivalent to 1,2,3,4,5
Let arr1 = [1,2,3,4,5] let arr2 = [6,7,8] let newArr = [...arr1,...arr2] //[].concat(arr1,arr2) console.log(newArr) //[1, 2, 3, 4, 5, 6, 7, 8] corresponds to concatCopy the code
In the ES6… Operators operate primarily on arrays, and in ES7 you can expand objects without going into too much detail here.
Welcome to point out any mistakes, thanks for watching.
Advertisement: author github: github.com/webxukai author gitee: gitee.com/webxukai author wechat: e790134972 author QQ: I think you should know! Author QQ mailbox: same as above, ha ha!