Three, Array
The array object adds some new static methods, and some new methods are added to the array prototype.
1.array.from to create an instance of an array from an array of classes and traversed objects
Class array objects include: arguments in Functions, NodeList objects returned by document.getElementsByTagName (), newly added map and set data structures.
In ES6 class array conversion method let Itemelements=document.queryselectorall ('. Item ') for arrays; let Items=array.from (itemelements); Items.foreach (function (item) { console.log (item.nodetype);}); /in ES5 class Array conversion method var Items=array.prototype.slice.call (itemelements);
In the above example, the class array object Itemelements object is not provided with the array method foreach (), but can be converted to an array by the Array.from () method.
Array.from () Another feature is its second optional parameter, mapfunction, which allows you to create a new map array with a single call:
Let Navelements=document.queryselectorall ("Nav li"); let Navtitles=array.from (Navelements,el=>el.textcontent);
2.array.of method
The method behaves much like the constructor of the array, which is suitable for passing only one parameter, so array.of is a better choice for the new array (), so there are now three ways to build the array:
Let X=new Array (3);//[Undefined,undefined,undefined]let Y=array.of (8);//[8]let z=[1,2,3];
The Find,findindex,fill method of 3.Array
1) Find returns the first element that returns true for a callback
2) FindIndex returns the subscript of the first element that returns true for a callback function
3) Fill the element with the given parameter ' overwrite ' array
[5,3,4,10,1].find (n=>n===10);//10[5,3,4,10,1].findindex (n=>n===10);//2[5,3,4,10,1].fill (7);//[7,7,7,7,7] [5,3,4,10,1].fill (7,1,3);//[5,7,7,7,1]
Iv. Math
Several new methods have been added to the Math object:
Math.sign returns the sign of a number with a result of 1,-1, or 0 math.sign (5);//1 math.sign ( -9)//-1//math.trunc returns a number with no decimal digits math.trunc ( 5.9);//5 Math.trunc (6.8908);//6//math.cbrt returns the cubic root of the number MATH.CBRT (64);//4
V. Extension operators
Extension operators are used to extend elements in specific places, such as extending an array of elements in an array.
Let Values=[1,2,4];let some=[...values,8];//[1,2,4,8]let more=[...values,8,... values];//[1,2,4,8,1,2,4]
Again, as in the arguments in the function call, apply:
Let values=[1,2,4];d osomething (... values); function dosomething (x, Y, z) { //x=1.y=2,z=4}// Call mode in ES5 dosomething.apply (null,values);
As you can see, this syntax frees us from the hassle of using fn.apply (), which is flexible because the extension operator is available anywhere in the parameter list.
We have seen the use of extension operators in arrays and parameters, in fact, we can use them in all comparable case objects:
Let form = Document.queryselectorall (' #my-form '). Inputs=form.queryselectorall (' input '); Selects=form.quryselectorall (' select '); let allthethings=[form,... inputs,... selects];
Now, Allthethings is a two-dimensional array containing <form> nodes, <input> child nodes, and <select> child nodes.
ES6 new features-------arrays and Math (cont.)