The Object type

1. Create a variable using the new operator followed by the Object constructor. 2. Use object literals.

Tips:

1. When an Object is defined through an Object literal, the Object constructor is not actually called (Firefox 2 and earlier call the Object constructor; But not after Firefox 3.)

2. Adding a comma after the last attribute causes an error in IE7 and earlier versions and Opera

Array type

Type conversion

All objects have toLocaleString(), toString(), and valueOf() methods, and toLocaleString() often returns the same value as toString(), but not always. When the array’s toLocaleString() method is called, it also creates a comma-separated string of array values. The only difference between the two methods is that instead of calling toString(), the toLocaletring() method of each item is called to get the value of each item.

If the valueOf an item in the array is null or undefined, the value is represented as an empty string in the result returned by the join(), toLocaleString(), toString(), and valueOf() methods.

Commonly used method

The stack method

The stack is a last-in-first-out (LIFO) data structure, In which the newest item is removed First, and the insertion (called push) and removal (called pop) of items In the stack occur In only one place — the top of the stack. Arrays also have stack-like operations:

The push() method can take any number of arguments, append them one by one to the end of the array, and return the modified array length,

The pop() method removes the last item from the end of the array, reducing the length value of the array, and then returns the removed item

Queue method

The access rule for queue data structures is FIFO (first-in-first-out). Queues add items to the end of the list and remove items from the front of the list. Arrays have a shift() method that ejects the first element of the array. Combined with push(), we implement a queue-like structure:

The shift() method removes the first item in the array and returns it, reducing the array length by one

The unshift method can take arbitrary arguments, add them to the array one by one, and return the modified length of the array

Unshift is a confusing point: if multiple arguments are passed in, they are inserted as blocks at the beginning of the object in the same order as when they were passed in as arguments. Thus, calling unshift once with multiple arguments and calling unshift multiple times with one argument (for example, loop calls) will get different results. For details, see mdN-unshift

Reorder method

Two methods already exist in arrays that can be used directly to resort: reverse() and sort(). By default, the sort() method sorts the items in ascending order — that is, the smallest value is first and the largest is last. To sort, the sort() method calls the toString() transformation method for each array item

Reverse () can reverse an array and return the sorted array.

The sort() method can take a comparison function as an argument so that we can specify which value comes before which. The comparison function takes two arguments and returns a negative number if the first argument should come before the second, 0 if the two arguments are equal, and a positive number if the first argument should come after the second.

function compare(value1, value2) { 
if (value1 < value2) { 
return 1; // When sorting, the larger one comes first
} else if (value1 > value2) { 
return -1; //// sort the smaller ones in the back
} else { 
return 0; }}var values = [0.1.5.10.15]; 
values.sort(compare); 
alert(values); / / 15,10,5,1,0
Copy the code
Operation method

The concat() method can create a new array based on all the items in the current array, slice() can create a new array based on one or more items in the current array, and splice() can delete any number of items or insert any number of items into a specified location.

Concat () creates a copy of the current array, appends the received arguments to the end of the copy, and returns the newly constructed array. Without passing arguments to the concat() method, it simply copies the current array and returns a copy. If the concat() method is passed one or more arrays, it adds each of those arrays to the result array. If the values passed are not an array, they are simply appended to the end of the result array

Slice () creates a new array based on one or more items in the current array. The slice() method can take one or two arguments to return the start and end positions of the item. In the case of a single argument, the slice() method returns all items from the position specified by that argument to the end of the current array. If there are two arguments, the method returns the items between the start and end positions — but not the items at the end position. The slice() method does not affect the original array, and if there is a negative number in the slice() method’s argument, the array length is added to that number to determine the position. For example, calling slice(-2,-1) on a 5-item array yields the same result as calling slice(3,4). If the end position is less than the start position, an empty array is returned

The main use of splice() is to insert items into the middle of an array, but there are three ways to use this method:

1. Delete: You can delete any number of items by specifying two parameters: the location of the first item to be deleted and the number of items to be deleted. For example, splice(0,2) removes the first two items in the array.

2. Insert: You can insert any number of items into the specified position by providing three arguments: the start position, 0 (number of items to delete), and the item to insert. If you want to insert more than one item, you can pass in a fourth, fifth, or any number of items. For example, splice(2,0,”red”,”green”) inserts the strings “red” and “green” from position 2 in the current array.

3. Replace: You can insert any number of items to the specified location and delete any number of items at the same time by specifying three parameters: the start location, the number of items to delete, and any number of items to insert. The number of inserted items need not be equal to the number of deleted items

Splice () changes the array

Location method

IndexOf () and lastIndexOf (). Both methods take two arguments: the item to look for and an (optional) index representing the starting point of the search, with indexOf() looking backwards from the beginning of the array (position 0) and lastIndexOf() looking forwards from the end of the array

An iterative approach

Every () : Runs the given function on each item in the array, and returns true if the function returns true for each item.

Filter () : Returns an array of items that return true by running the given function on each item in the array.

ForEach () : Runs the given function on each item in the array. This method returns no value.

Map () : Runs the given function on each item in the array, returning an array of the results of each function call.

Some () : Runs the given function on each item in the array, returning true if the function returns true for any item.

None of the above methods modify the values contained in the array.

Merge method

Reduce () and reduceRight (). Both methods iterate over all the items in the array and then build a final value that is returned. In this, the reduce() method starts at the first entry of the array and iterates through to the end. ReduceRight (), on the other hand, starts from the last item of the array and traverses forward to the first item.

The Date type

The RegExp type

Re declaration:

Var expression = / *pattern* / *flags*;

Var pattern2 = new RegExp(“*pattern*”, “*flags*”);

When used, you cannot pass the literal’s re to the constructor entirely. Since the RegExp constructor’s schema argument is a string, characters are double-escaped in some cases. All metacharacters must be doubly escaped, as must characters that have already been escaped, such as \n (the character \ is usually escaped as \ in strings, but becomes \\ in regular expression strings).

RegExp instance property

Global: A Boolean value indicating whether the G flag is set.

IgnoreCase: Boolean value indicating whether the I flag is set.

LastIndex: integer representing the character position at which the search for the next match begins, counting from 0.

Multiline: Boolean value indicating whether the M flag is set.

Source: String representation of the regular expression, returned as a literal rather than as a string pattern in the passed constructor.

RegExp instance method

Test () : takes a string argument. Returns true if the pattern matches this parameter; Otherwise, return false.

Exec () : takes an argument, the string to which the pattern is to be applied, and returns an array containing information about the first match; Or return NULL if there is no match.

ValueOf of a regular expression returns the regular expression itself.

Without the global flag set, multiple calls to exec() on the same string will always return information about the first match, lastIndex, unchanged. With the global flag set, each call to exec() will continue looking for a new match in the string.

The lastIndex of the regex object was reset. This global flag was the cause of the problem

The Function type

Overloading: JavaScript is not overloaded, so methods with the same name are overwritten. In JavaScript, everything is an object, and the function name can be treated as a pointer, so it is overwritten when declared again.

Function declaration and function: when the parser to load the data in the execution environment, lead the read function declarations () function declarations ascension, and make it available before perform any code (access), function, must wait until the parser is executed to the lines of code, it is in that would truly be interpreted execution.

Inside the function, there are two special objects: Arguments and this

This function takes two arguments length and prototype