This is an interview question, and for this question, we need to explain what an array of classes is.

An array of class

The definition of a class array is very complicated in the JavaScript standard. It has the following characteristics:

  1. It can access the current element with an index value and know the length of the current array
  2. It cannot use other array methods such as map,filter, etc

Class arrays are usually used more for function arguments, arguments being an array of classes

Common methods for arrays and objects

Following the old way of saying the difference, we first need to carefully explain the corresponding method of the two so that the difference is clear at a glance. So to start, let’s start with common methods for objects:

Obj = {a: 1, b:"1",c:true,d:{}} for... obj = {a: 1, b:"1",c:true,d:{}} for... in ... HasOwnProperty returns a Boolean value, ToString -- defineProperty -- valueOf -- return the original valueOf this object // {a: 1, b: "1", c: true, d: {... }} for... of... -- Can iterate over objects with iterators, Map, for example, set, the array keys and values - were returned by this object is an array of attributes and attribute values of / / keys: {" a ", "b", "c", "d"} values: {1, "1", true, {... }}Copy the code

Then there are the common methods of arrays

The first is the usual operation on the array itself: shift - removes elements from the head and returns deleted elements unshift - adds elements from the head and returns no value pop - deletes elements from the tail and returns deleted elements push - adds elements from the tail and returns no value Concat - joins multiple arrays into one array and returns the merged array splice - can pass in multiple arguments. If one or two items are changed, the value of one or two items is returned. If one or two items are changed, the value of one or two items is returned. The first parameter is the starting position of the deleted element, and the second parameter is the number of deleted elements. If you exceed the number of deletions, which is all deletions and the third parameter is the number of deletions that you want to add, For example, [1,2].splice(0,1,2,2) returns [2,2] reverse -- sort the array in ascending order but can be changed to descending order by passing the function The return value is the changed array, and this method causes the original array to be changed slice - splits a portion of the original array and returns it as a return value, such as [1,2,4,5,6]. The value for slice(1,3) is [2,4]. The first argument is the start position, the second argument is the end position, Join -- joins the array according to the value added to the array. The return value is the value returned after joining. Filter -- iterates through the array according to the given callback function ForEach -- traverses the entire array and returns undefined map -- traverses the entire array according to the callback function and returns an array of the elements called every,some -- The value returned is a Boolean value reduce -- to accumulate an array, you need at least one cumulative value, one current value, You can also pass the current index value and the entire source array, return a cumulative value find -- finds if the entire array contains a value, returns the index of that value if it does not return undefind valueOf -- finds if the array contains a value, returns the index of that value if it does, If no -1 for... of... - with the object for... in... -- Same as Object keys,values -- same as ObjectCopy the code

Class array and array interconversion

Array itself is an object, he has all the methods of class array, so array to class array without thinking, here just talk about the conversion of class array:

ES6
1.const newArr = [...arguments]
2.Array.from
Copy the code
ES5
function toArray(s){ var arr = []; try{ arr = Array.prototype.slice.call(s) }catch{ for(var i=0; i < s.length; i++){ arr.push(s[i]) } } return arr }Copy the code