Built-in objects in JavaScript's Getting Started JS

Source: Internet
Author: User
Tags string format

First, array 1, the basic concept of arrays

An array is a collection of ordered data that is stored continuously in memory space. The order of the elements in the array, called subscripts. You can use the subscript to access each element of the array.

2. How to declare an array

① uses literal declaration: var arr = []; in JS, the same array can store various data types; Eg:var arr = [1, "Wuhao", True,{},null,func]
② Use the New keyword declaration: var arr = new Array (parameter);
The >>> parameters can be:
A. parameter ellipsis, which means creating an empty array
B. The parameter is an integer that declares an array of length of the specified size. However, this length can be variable at any time to append
C. Multiple numeric values separated by a comma, representing multiple value of the array. New Array (All-in-a--) = [[+]

3. Read/write/delete of elements in array

① Read and write: Access elements by subscript. Subscript starting from 0 arr[1] = "haha";
② Additions and deletions:
A. Use the Delete keyword to delete one of the values of the array. After deletion, the length of the array is unchanged, and the corresponding position becomes undefined. Eg:delete Arr[1];
B.arr.pop (): Deletes the last value of the array. Equivalent to Arr.length-= 1;
C.arr.shift (): Deletes the first value of the array.
D.arr.unshift (value): Adds a value in the No. 0 position of the array;
E.arr.push (value): Adds a value to the last position of the array;
F. Direct access to an array of subscript not reached, can be dynamically appended.
eg:arr[8]= "hehe"; Arr[100]=1; In the middle if there is empty remaining mark, will be deposited undefined.

4, the array of other methods (very very popular!!!!) Interview Topic Special LOVE TEST!!! Especially sort)

①Join (): links the array with the specified delimiter as a string. When the argument is empty, the default is separated by commas.

The result of the operation is:

②concat (): [The original array will not be changed] link the array to a new array with the values of two or more arrays. Concat If there is a two-dimensional array, you can remove at most one layer []
Eg:[1,2].concat ([3,4],[5,6]); -->[1,2,3,4,5,6]
[1,2].concat ([3,4,[5,6]]); -->[1,2,3,4,[5,6]]
③push (): adds a unshift () at the end of the array: adds one to the beginning of the array and returns the length of the new array pop (): At the end of the array, delete a shift (): The beginning of the array deletes one--returns the deleted value;
[The above method will change the original array]

The result of the operation is:

④Reverse (): [The original array is changed] flips the array and outputs the flashback.
Eg:var arr = [1,2,3,4,5,6,7,8];
Arr.reverse ();
Console.log (arr);

The result of the operation is:

⑤Slice (begin,end): [The original array will not be altered] intercepts a part of the array and returns a new array that is intercepted.
>>> Pass in a parameter that represents the start interval, which is truncated to the end of the array by default;
>>> incoming two parameters, indicating the start and end subscripts, left closed right open interval (contains begin, does not contain end);
>>> two parameters can be negative, indicating the number from the right, the last value is-1;

The result of the operation is:

⑥sort (): [The original array is changed] arranges the array in ascending order;
>>> by default, the ASCII value is sorted by the first letter of each element;
Eg:[1,5,13,12,7,6,4,8].sort ()-->[1,12,13,4,5,6,7,8];
>>> can pass in a comparison function, manually specify the sorting function algorithm;
The function will receive a value of two by default, a, B, if the function returns a value of >0, it proves a>b, and if the function returns a value of <0, the A<b
Arr.sort (function (b) {
Return a-b;//in ascending order;
return b-a;//descending order;
});

⑦indexOf (value,index): Returns the subscript of the first value value in the array, and returns -1;lastindexof (Value,index) If not found: Returns the subscript that contains the last value value in the array, Returns 1 if it is not found;
>>> If index is not specified, all groups are searched for value;
>>> If index is specified, the value is searched backwards, starting with index;

The result of the operation is:

⑧ForEach (): Designed to iterate through an array. Receive a callback function, the callback function receives two parameters, the first parameter is the value of each item of the array, the second parameter is subscript (IE8, this function is not supported!!!!! )
var arr = [1,5,6,3,4,2,7,6,4,8];
Arr.foreach (function (item,index) {
Console.log (item);
})

The result of the operation is:

⑨Map (): array mapping, using the same method as foreach (). The difference is that the map can have a return value, indicating that each value of the original array is manipulated and returned to a new array. (This function is not supported prior to IE8!!!!! )
var arr1 = Arr.map (function (item,index) {
Console.log (item);
return item+2;
});
Console.log (ARR1);

5, two-dimensional arrays and sparse arrays (understanding)

① two-dimensional array: The value in the array is still an array form.
eg:arr=[[1,2,3],[4,5,6]]; Equivalent to two rows of three columns, reading two-dimensional array: arr[[column];
② Sparse Array: The index in the array is not contiguous. (length is greater than the actual number of elements in the array)

The result of the operation is:

6. Basic data types and reference data types

① Basic Data type: When assigning a value, the value in the original variable is assigned to another variable, and after the copy is completed, two variables are independent of each other, modifying one of the values, and the other does not change.

② Reference data type: When assigning a value, the original variable is assigned to another variable in the memory address. When the assignment is complete, the same memory address is stored in two variables, and the same data is accessed, one of which changes the other.
③ variables such as numeric, String, Boolean are basic data types; Arrays, objects are reference data types;

The result of the operation is:

Second, built-in Object Boolean class (Boolean Class)

There are two ways to declare it: You can declare a simple variable by literal means; Use typeof to detect a Boolean type, or you can declare a Boolean object with the new Boolean (); The object type is detected with TypeOf.

The result of the operation is:

Number class

Number.MAX_VALUE: Returns the maximum value that the number class can represent
Number.min_value: Returns the minimum value that the number class can represent

. ToString (): Converts a numeric type to a string type;

The result of the operation is:

. toLocaleString (): Converts a numeric value to a string in the local format order, usually starting from the right, with three groups separated by commas;

The result of the operation is:

. toFixed (n): Keep number n decimal and convert to string format;

The result of the operation is:

. Toprecision (n): Formats the number as a specified length; n denotes the length of the digits without a decimal point. If the n< is the length of the original number, it is represented by scientific notation. If n> the original number length, then 0 after the decimal point;

The result of the operation is:

. ValueOf (): Returns the base numeric value of the number object;

The result of the operation is:


String class

1, attribute: Str.length Returns the length of the string, that is, the number of characters. The string supports an array-like subscript access method: str. [0];
2. Method (Very important)
. toLowerCase (): Converts all characters of a string into lowercase;
. toUpperCase (): Converts all characters of a string into uppercase;
. CharAt (n): Intercept the array of nth arrays, equivalent to str[n];
. IndexOf ("str", index): From the index position, find the position where the substring appears in the string, if no return-1 is found, and the other IndexOf method from the array;
. LastIndexOf ("str", index): same array;
. Substring (begin,end): Intercepting substrings from a string
>>> pass in only one parameter, which means starting from begin, to the last;
>>> passed two parameters, indicating the interval between begin and end, left closed right open interval;
. Split ("delimiter"): Separates the specified delimiter string into the array. Empty "" means that each character of a string is put into an array separately;
. Replace ("old", "new"): Replaces the first old of a string with new.
>>> The first parameter can be either a normal string or a regular expression;
>>> if it is a normal string, only the first old is replaced. If it is a regular expression, it can be replaced according to the requirement of the regular notation;

Date Day Class

1. New Date (): Returns the current time; new Date ("2017,8,31,12:34:56"); Returns the specified time
2. Common methods:
. getFullYear (): Gets the 4-bit year;
. GetMonth (): Get month 0~11
. GetMonth (): Gets the day of the first one months 1~31
. GetDay (): Gets the day of the week 0~6,0 represents Sunday
. Dates.gethours (): Get Hours
. getminutes (): Get minutes
. Getseconds (): Get Seconds

How to make a dynamic date

The result of the operation is:

Iii. Custom Objects

1. Basic Concepts
① object: An object is a collection that has a series of unordered properties and methods.
② Key-value pairs: The data in an object exists in the form of a key-value pair. Each property and method of the object corresponds to a key name, which is the key value.
③ Property: A series of variables that describe the characteristics of an object, called a property. [Variables in Object]
④ method: A series of functions that describe the behavior of an object, called a method. [Functions in Objects]
2. Declaration of objects
① literal declaration: var obj = {
Key1:value,
Key2:value,
Func:function () {},
}
The data in the >>> object is stored as a key-value pair, separated by a key from the value: Multiple key-value pairs are separated from each other.
The keys in the >>> object can be any data type except for arrays/objects. But generally we only use the common variable name as the key.
The values in the >>> object can be any data type, including arrays and objects.
② using the New keyword declaration: var obj = new Object ()
Obj.key1 = value1,
obj.func1= function () {}
3. Read and write the properties and methods in the object:
①. operator
Object inside: this. Property this. method (); External object: Object name. Property object name. method;

② through ["Key"] Call: Object name. The name of the [property name] object. ["Method Name"] ();
>>> if the key includes special characters, you can only use the ② method;
>>> object, write the variable name directly, and the default is to call the global variable. If you need to call the properties or methods of the object itself. You need to use the object name. property, or this. property. Person.age This.age. Yes, but it is recommended to use the This keyword;

③ deleting an object's properties and methods: Delete Object name. Property name/Method name. Delete Person.age;
    

Built-in objects in JavaScript entry JS

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.