Home >Web Front-end >JS Tutorial >Summary of JavaScript basic knowledge methods_javascript skills
Array methods:
array.concat concatenates an array with another array and returns a composite array. var arrC=arrA.concat(arrB,'asd','sad',true,1.5);
array.join concatenates the array into a string using the specified symbol and returns the string. Much faster than using +. var strA=arrA.join(",");//Default is comma
array.pop removes the last element of the array and returns this element.var A=arrA.pop();
array.push appends one or more arguments to the end of the array and returns a new length value. arrA.push("asd");
array.shift removes the first element of an array and returns this element. It is much slower than pop. I think the reason is that this is just a pseudo array. To delete the first one, all elements must be pushed forward one space. var A=arrA.shift();
array.unshift appends one or more arguments to the head of the array. arrA.unshift("asd");
array.reverse reverses the order of elements in an array. var arrB=arrA.reverse();
array.slice makes a shallow copy of the array. Then assign the array between the two subscripts and return a new array. var arrB=arrA.slice(0,3);//Take out the 0th, 1st, and 2nd elements, and the following 3 can also be left blank
array.sort sorts the contents of an array. arrA.sort();//By default, all elements will be converted into strings and compared. Of course, you can also pass a comparison function as a parameter
arrA.sort(function(a,b){
return a-b;
});
array.splice removes one or more elements from an array and replaces them with new elements. var arrB=arrA.splice(0,3,'asd','ads');//Remove the 3 elements starting from index 0 and replace them with the next two elements
Function method:
function.apply calls a function, passing an object bound to this and an optional array as the parameter array.
Number method:
number.toExponential Converts this number to an exponential string. Math.PI.toExponential(2);//3.14e+0
number.toFixed Converts this number to a string in decimal form. Math.PI.toFixed(2);//3.14, keep two decimal places
number.toPrecision Converts this number to a string in decimal form. Math.PI.toPrecision(2);//3.1, keep two valid figures
number.toString Converts this number to a string. Math.PI.toString(2);//The 2 here represents the base, not the precision
Object methods:
object.hasOwnProperty determines whether the object contains a property named with the specified string
Regular expression method:
regexp.exec
If there is a successful match, an array will be returned. Subscript 0 will return the matching original string, and 1~ will return the text captured by group 1~.
With the g flag (global flag), the search does not start from the beginning of the string, but from regexp.lastIndex. If the match is successful, regexp.lastIndex will be set to the position of the first character of the successfully matched string, otherwise it will be reset to 0.
regexp.test returns true if the match is successful, otherwise false
String methods:
string.charAt returns the character at the specified position in the string
string.charCodeAt returns the ASCII code value of the character at the specified position in the string
string.concat concatenates other strings together, returning a new composite string. In fact, using + is more convenient and intuitive.
string.indexOf searches for another specified string in this string. If it is found, it returns the position of the first found string, otherwise it returns -1."asdasd".indexOf("sd",2); //The value is 4, 2 means starting to search from the 3rd character
string.lastIndexOf This is similar to the above, except that it searches from the end of the string
string.localeCompare compares two strings. strA.localeCompare(strB);//The result also returns positive numbers, negative numbers, zero, you know
string.replace performs a search and replace operation on a string and returns a new string (the following methods marked in red can apply regular expressions)
Conventional method:
'asdasd'.replace('as','d');//The result is ddasd, replace will only replace the first occurrence.
Regular expression method:
Add the g logo to match multiple times, and do not add it to match once
var regExp=/ee(asdd{0,3})/g;//Add g mark to match multiple times, do not add one match
var p='eeasd1323'.replace(regExp,'$1end');//The result is eeasd1323end
//You can also put a function at the position of '$1end'. This function will be run every time it is matched, and then replaced with the return value of the function. I won’t give an example here
The explanation of '$1end' is as follows:
$$: represents the symbol $
$&: represents the entire matched text of $&
$number: represents the text captured by the group. For example, $1 above is the text captured by capture group 1
$`: Match the previous text
$': Text after matching
string.match matches a string with a regular expression. It determines how to match based on the g flag.
If there is no g flag, the matching result is the same as regexp.exec
If so, then an array will be generated that contains all matches (except for the capture group, I don’t understand what the parentheses mean, but it obviously matches all)
string.search is similar to indexof, except that it receives a regular expression match. This method ignores the g flag.
string.split Splits a string to create an array of strings. This method ignores the g flag.
General gameplay
var digits='0123456789';
var arr=digits.split('',5);//5 means that the obtained array has at most five elements, and the excess ones are removed
//The result is ['0','1','2','3','4']
How to play with regular expressions
var text='troy ,123 , good ';
var d=text.split(/s*,s*/);//As mentioned earlier, s represents various null characters in unicode, and the matching delimiter is a comma containing null characters, so the null characters are automatically removed. So powerful
//But there are special cases. The text from group capture will be included in the separated characters, so I personally suggest not to use group capture. There is no need here
string.slice copies part of a string to construct a new string
string.substring has the same effect as slice, except that negative subscripts cannot be used. There is no reason to use substring instead of slice (said the author). In fact, there is. For example, I know what it means literally.
string.toLowerCase returns a new string that is all lowercase.
string.toLocaleLowerCase Same as above, only for Turkish, so just pretend you can’t see it
string.toUpperCase returns a new string in all uppercase letters.
string.toLocaleUpperCase Same as above, only for Turkish, so just pretend you can’t see it
String.fromCharCode is promising, string is uppercase. So instead of calling after the string, call it with String. Returns a string based on a numeric encoding. (I believe you basically don’t need it)