Es6 new knowledge

Source: Internet
Author: User
Tags maximum math

... Operator

Convert an array into a comma-separated parameter sequence.

 
Function push (array ,... items) {array. push (... items);} function add (x, y) {return X + Y;} VAR numbers = [4, 38]; add (... numbers );

Function calls with variable parameters

Function push (array ,... items) {array. push (... items);} function add (... vals) {Let sum = 0; For (let I = 0; I <Vals. length; I ++) {sum + = Vals [I];} return sum;} Let arr = [1, 2, 4, 5, 6]; let sum = add (... ARR); console. log (SUM); // 21

More convenient array Merging

 
Let arr1 = [1, 2]; let arr2 = [5, 6]; let newarr = [20]; // newarr = newarr in the old es5 statement. concat (arr1 ). concat (arr2); // [20, 1, 2, 5, 6] Console. log (newarr); // es6 use the extension operator newarr = [20 ,... arr1 ,... arr2]; // [20, 1, 2, 5, 6] Console. log (newarr );

Replace the apply method of es5

// Es5 function f (x, y, z ){//...} vaR ARGs = [0, 1, 2]; F. apply (null, argS); // function f (x, y, z) Written in es6 ){//...} vaR ARGs = [0, 1, 2]; F (... ARGs );

Maximum math. Max ()

 
// Es5 statement math. max. apply (null, [14, 3, 77]) // write math for es6. max (... [14, 3, 77]) // equivalent to math. max (14, 3, 77 );

Add an array to the end of another array through the push function

 
// Es5 syntax var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; array. prototype. push. apply (arr1, arr2); // write var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; arr1.push (... arr2 );

New date type

// Es5 new (date. BIND. apply (date, [null, 2015, 1, 1]) // es6 new date (... [2015, 1, 1]);

Combined with deconstruct assignment to generate a new array

 
// Es5 A = list [0], rest = List. Slice (1) // es6 [A,... rest] = List

The following are some other examples.

 
Const [first ,... rest] = [1, 2, 3, 4, 5]; first/1 rest // [2, 3, 4, 5] const [first ,... rest] = []; first // undefined rest // []: const [first ,... rest] = ["foo"]; first // "foo" Rest // []

Convert a string to a real Array

 
[... 'Hello'] // ["H", "E", "L", "L", "O"]

Convert objects that implement the iterator interface into Arrays

 
VaR nodelist = Document. queryselectorall ('div '); var array = [... nodelist];

New es6 knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.