1. Create an array
The simplest way to create an array is to add an object directly in JavaScript code. The direct object quantity is a list of attribute names and values separated by commas (,). Each attribute name can be a javascript identifier or a string, each property value can be a constant or any JavaScript expression.
The new operator can create a specific class of objects. After new, a constructor is called to initialize the attributes of an object.
2. Objects used as correlated Arrays
We can use the operator "." to access an object attribute, while the more common access attribute operator of arrays is []. In this way, the values of the following two expressions are equal:
Object. Property
Object ["property"]
The most important difference between these two syntaxes is that the property name of the former is the identifier, and the property name of the latter is a string.
3. Common Object Attributes and Methods
All objects in JavaScript are inherited from the object class.
3.1 constructor attributes
In JavaScript, each object has a constructor attribute, which references the constructor that initializes this object.
3.2. tostring () method
This method has no parameters. It returns a string that represents the value of an object to some extent, and it is called on this object. This method is called when JavaScript needs to convert an object into a string.
3.3. tolocalestring () method
This method is used to return a localized string representation of an object. By default, this method does not localize itself. It always returns exactly the same content as tostring.
3.4 valueof () method
When JavaScript needs to convert an object to a certain basic data type, that is, a number rather than a string, it calls a method. If an object is used in an environment that requires a basic value, javascript will automatically call this method.
3.5 hasownproperty () method
If the object uses the name specified by a separate string parameter to locally define a non-inherited attribute, this method returns true. Otherwise, false is returned.
3.6. propertyisenumerable () method
If the object uses the name specified by a separate string parameter to define a non-inherited attribute, and this attribute can be enumerated in a for/in loop, propertyisenumerable () returns true. Otherwise, false is returned.
3.7 isprototypeof () method
If the object to which the method belongs is the prototype object of the parameter, the method returns true. Otherwise, false is returned.
4. Array
The simplest way to create an array is to use the array directly, which is only a list of array elements separated by commas in square brackets. The values in the direct quantity of an array are not necessarily constants. They can be any expressions. The direct quantity of arrays can contain the direct quantity of objects or other arrays.
Another way to create an array is to use the Array () constructor: No parameter call; you can explicitly specify the values of the First n elements of the array; you can pass it a numeric parameter, this number specifies the length of the array.
4.1 Delete array elements
Delete operator. An array element is set to the undefined value, but the element still exists. To delete an element so that all the elements whose subscript position is higher than it are migrated down to a lower subscript position, an array method must be used. Array. the shift () method deletes the first element of the array, array. the pop () method deletes the last element, array. splice () Removes elements in a continuous range from an array.
5. Array Method
5.1 join () method
This method can convert all elements of an array into strings and then connect them. You can specify an optional string to separate the elements in the result string. If no separator string is specified, use commas to separate elements.
5.2. Reverse () method
This method will reverse the order of array elements and return the inverted array. It performs this operation on the meta array. That is to say, it does not create a new array for elements rearranging, but rearranges the array elements in an existing array.
5.3. Sort () method
This method sorts array elements in the original array and returns the sorted array. If the parameter is not set during the call, the array elements are sorted alphabetically.
5.4 Concat () method
This method can create and return an array, which contains the elements of the original array that calls Concat (), followed by the Concat () parameter. If some of the parameters are arrays, they are expanded and their elements are added to the returned array. Note: This function does not recursively expand an array whose elements are arrays.
5.5 slice () method
This method returns a segment of the specified array, or a sub-array. Its Two Parameters specify the start and end points of the fragments to be returned. The returned array contains the elements specified by the first parameter and the elements starting from the element to the element specified by the second parameter, but does not contain the elements specified by the second parameter.
5.6. splice () method
This method is a common method for inserting or deleting array elements. It modifies the array on the original array and does not create a new array.
5.7. Push () and POP () Methods
These two methods allow us to use arrays just like using stacks.
5.8. unshift () and shift () Methods
The actions of these two methods are very similar to those of push () and POP (), except that they insert and delete elements in the header of the array, rather than at the end.
5.9. tostring () and tolocalestring () Methods
The tostring () method can convert each element of an array into a string.
Tolocalestring () is the version of tostring () method localization. It will call the tolocalestring () method of each element to convert the array element into a string, and then connect the generated string with a local specific separator string.