Summary of basic JavaScript knowledge-javascript tips-js tutorial

Source: Internet
Author: User
This article shares with you the basic knowledge of javascript, including array, function, numeric, object, String, regular, and regular expressions, this article is very detailed and has reference value for your reference. Array method:

Array. concat concatenates one array to another and returns a compositing array. Var arrC = arrA. concat (arrB, 'asd ', 'sad', true, 1.5 );
Array. join concatenates an array into a string with the specified symbol and returns the string. It is much faster than usage +. Var strA = arrA. join (","); // The default value is comma.
Array. pop removes the last element of the array and returns this element. var A = arrA. pop ();
Array. push attaches one or more parameters to the end of the array and returns a new length value. ArrA. push ("asd ");
Array. shift removes the first element of the array and returns this element. It is much slower than pop, because I think this is just a pseudo array. deleting the first one will push all the elements forward. Var A = arrA. shift ();
Array. unshift attaches one or more parameters to the header of the array. ArrA. unshift ("asd ");
Array. reverse reverses the order of elements in the array. Var arrB = arrA. reverse ();
Array. slice performs a partial copy on the array. Then assign an array between two subscripts to return a new array. Var arrB = arrA. slice (); // extracts the first, second, and second elements, and the third element can be left blank.
Array. sort sorts the content in the array. ArrA. sort (); // by default, all elements are 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 the array and replaces them with new elements. Var arrB = arrA. splice (, 'asd ', 'analytics'); // remove the three elements starting with subscript 0 and replace them with the following two elements.

Function Method:

Function. apply calls the function and transmits an object bound to this and an optional array as the parameter array.

Numeric method:

Number. toExponential converts the number into an exponential string. Math. PI. toExponential (2); // 3.14e + 0
Number. toFixed converts the number into a string in decimal format. Math. PI. toFixed (2); // 3.14, retain two decimal places
Number. toPrecision converts the number to a string in decimal format. Math. PI. toPrecision (2); // 3.1, retain two valid numbers
Number. toString to convert the number into a string. Math. PI. toString (2); // here, 2 indicates hexadecimal instead of accuracy.

Object method:

Object. hasOwnProperty: determines whether the object contains an attribute named after a specified string.

Regular Expression Method:

Regexp.exe c
If the match is successful, an array is returned. Subscript 0 returns the matched original string, 1 ~ Group 1 ~ The captured text.
If it has a g ID (Global ID), the search starts from regexp. lastIndex instead of the starting position of the string. If the match is successful, regexp. lastIndex is set to the first character of the matched string. Otherwise, it is reset to 0.
If regexp. test Matches successfully, true is returned; otherwise, false is returned.

String method:

String. charAt returns the character at the specified position in the string.
String. charCodeAt returns the ASCII value of the character at the specified position in the string.
String. concat connects other strings and returns a new synthetic string. In fact, it is more convenient and intuitive to use +.
String. indexOf searches for another specified string in this string. If yes, the position of the first string is returned. Otherwise,-1 is returned. "asdasd ". indexOf ("sd", 2); // The value is 4 or 2, indicating that the search starts from 3rd characters.
String. lastIndexOf is similar to the above, but it is searched from the end of the string.
String. localeCompare compares two strings. StrA. localeCompare (strB); // The result also returns positive, negative, zero, you know
String. replace: searches for and replaces a string, and returns a new string (the following method marked in red can apply a regular expression)

General method:

'Asdasd'. replace ('as', 'D'); // The result is ddasd. replace will only replace the first occurrence location.

Regular Expression Method:

The g ID is added for multiple times without a match.

Var regExp =/ee (asd \ d {0, 3})/g; // Add g ID multiple times, no match once
Var p = 'eeasd1323'. replace (regExp, '$ 1end'); // The result is eeasd1323end.
// You can also place a function at the '$ 1end' position. This function will be run each time it is matched, and then replaced with the return value of the function. Here we will not explain it.
'$ 1end' is explained as follows:
$: Indicates the $ symbol.
$ &: Indicates $ & the entire matched text
$ Number: indicates the text captured by the group. For example, the above $1 indicates the text captured by the capture Group 1.
$ ': Match the previous text
$ ': Matched text
String. match matches a string with a regular expression. It determines how to match based on the g ID.
If there is no gflag, the matching result is the same as regexp.exe c.
If yes, an array containing all the matches is generated (except for the capture group, the strings in this bracket do not understand what they mean, and they match all of them ).
String. search is similar to indexof, but only receives a regular expression matching. This method ignores the g ID.
String. split splits the string to create a string array. This method ignores the g ID.
General gameplay
Var digits = '20140901 ';
Var arr = digits. split ('', 5); // 5 indicates that at most five elements of the obtained array are removed.
// The result is ['0', '1', '2', '3', '4'].
Regular Expressions
Var text = 'troy, 123, good ';
Var d = text. split (/\ s *, \ s */); // As mentioned earlier, \ s indicates various empty characters in unicode. Therefore, the matching separator is a comma containing null characters, in this way, empty characters are automatically removed, which is very powerful.
// But there is a special case. The text captured by the group will be contained in the separated characters. Therefore, I personally recommend that you do not use the group to capture the text. It is unnecessary here.
String. slice copies a part of the string to construct a new string.
String. substring has the same effect as slice, but cannot use negative subscript. There is no reason to use substring instead of slice ). Actually, there are some. For example, I know what it means literally.
String. toLowerCase returns a new lowercase string.
String. toLocaleLowerCase is the same as above. It is dedicated to Turkish, so you should be invisible.
String. toUpperCase returns a new capitalized string.
String. toLocaleUpperCase is the same as above, which is dedicated to Turkish, so you should be invisible.
String. fromCharCode is optimistic. string is capitalized. Therefore, it is called not after a String, but with a String. Returns a string encoded by a number. (I believe you cannot use it)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.