ES6 Added Create Method (Summary)

  • 的头像-Tumi
  • 36 day agopublish

Before ES6, there are two ways to create arrays:

   

let array = [1, 2, 3]; console.log (array); // [1, 2, 3]

  2: through new Array () Create an array  

Let Array = New Array (1, 2, 3); Console.log (array); // [1, 2, 3]

In most cases, New Array () is good:
   
Let Array = New Array ( 1, 2); console.log (array.length); //2console.log (Array [0]); //1console.log (Array[1]); // 2aRRAY = New Array (‘a’); Console.log (array.length); //1console.log (Array[0]) ;/’a’a’ = new array (1, ‘a’); console.log (array.length); // 2console .log (array [0]); // 1console.log (array [1]); // ‘a’

But new array () has a strange Situation:
   
Let Array = New Array (2); Console.log (ArrAY [0]); // UndefinedConsole.log (array [1]); // UndefinedConsole.log (array.length); // 2


When we give NEW ARRAY () When a single numeric parameter is transmitted, this number is not an array element, but the array’s length attribute, and the array itself is an empty array.


In order to solve the above-mentioned characteristics, ES6 introduces Array.Of () to solve this problem:


Three: Array.

As the name suggestion, the OF () method is to create an array with the parameters it accepts as an element. The situation we say above is also applicable:

 let array = array.of (3); console.log (array.length); // 1console.log (array [0]); // 3Array = array.of (1, 2); console.log (array.length); // 2console.log (array [0]); // 1console.log (array [1]); // 2Array = array.of ('a' ); console.log (array.length); // 1console.log (array [0]); // 'aaaRray = array.of (1,' a '); console.log (array.length); // 2console.log (array [0]); // 1console.log (array [1]); // 'a'   


ES6 also added an array.from (), but also used an array. It is mainly used to create a corresponding array in class with class array objects and overlapping objects.

1: Array.From (class number object)

We are most familiar with class array objects should be the Function’s Arguments object. Next, let’s see an array containing array.from ():

function creteArrayFrom () {console.log (arguments instanceof array); // False Return Array.From (arguments);} let array = creteArrayFrom (1, 2, 3); console.log (array instanceof array); // true.Log (array.length); //3console.log (array [array ” 0]); // 1console.log (array [1]); // 2 Console.log (array [2]); // 3console.log (array.indexof (2)); // 1
  2: Array.From (iterable object)  
Array.From () can also convert an iterative object to an array :

Let iteratorObject = {* [Symbol.iteraTOR] () {yield 1; yield 2; yield 3;}}; let array = array.from (item); console.log (array instanceof array); // true.Log (array.length); // 3console .log (array [0]); // 1

5: ARRAY.FROM () second parameter


The previous example, we see a class array object and an iterative object as the first parameter of Array.From (), thereby creating an array containing their elements. The second parameter of Array.FROM () is a function that is used to change the elements of the class array object and the iterative object, and then act as an element of the eusence, for example:
   Let itemObject = {* [symbol.iterator] () {yield 1; yield 2; yield 3;}}; let array = array.from (item) => {Return Item + 1}); console.log (Array [0]); //2console.log (Array [1]); //3console.log (Array [2]); // 4 

In this example, we provide a transformation of each element value, so after the original 1, 2, 3, after replacing the new array, the element is 2, 3, 4.
Six: ARRAY.FROM () third parameter

Array.From () also provides the third parameter available, the third parameter is used to specify the value of this, if you use it to this, you can use an example:
  Let firsthelper = {DIFF: 1, add (value) {Return Value + this.diff;}}; let secondHelper = {DIFF: 2}; function createArrayFrom () {Return Array .from (arguments, firsthelper.add, secondhelper);} let array = creterrayfrom (1, 2, 3); console.log (array); // [3, 4, 5]  
Inside the above example, our use of this (this line code: value + this.diff) in the add () method, and add () defines in the firsthelper object, and the firsthelper object also has a Diff property, But our third parameter is incoming SECondHelper, so the THIS value in the firsthelper.add () method is Secondhelper.

The above is the new Array.Of () and array.from () methods, which can make developers to creating more events with fewer creating arrays.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment