JS arrays, built-in objects, custom objects

Source: Internet
Author: User
Tags string format

"Arrays in JS"
1, the basic concept of the array?
An array is a set of ordered data that is stored continuously in memory space .
The order of the elements in the array, called subscripts. You can use the following table to access each element of a number.

2, how to declare an array?
① use literal declaration:
in JS, the same array can store various data types.
For example: Var arr=[1, "Satan", True,4,func,{},null];
② using the New keyword declaration: var arr=new Array (parameter);
the >>> parameters can be:
A. Omit the parameter, which means to create an empty array;
B. The parameter is an integer that declares an array of length of the specified size. But this length can be added at any time;
c. Multiple numeric values with a comma-delimited parameter. Represents multiple values of an array;
new Array (==[1,2,3)
3. Read/write/delete elements in the array?
① Read and write: Accesses elements through the following table. Subscript starting from 0 arr[1]= "haha";
② Additions and deletions:

Delete: A. Use the Delete keyword to remove a value from an 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.

Increment: D.arr.unshift (value): Adds a value to 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 the subscript that the array does not reach, can be dynamically appended.
Arr[100]=1, 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 to a string with the specified delimiter. When the argument is empty, the default is separated by commas.
②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 (): Add one at the end of the array, Unshift (): Add one at the beginning of the array. --Returns the length of the new array;
pop (): Deletes one at the end of the array, and SHIFT (): Deletes one at the beginning of the array. --Returns the deleted value;
[The above method will change the original array]

④reverse (): [The original array is changed] flips the array and reverses the output.
Eg:var arr = [1,2,3,4,5,6,7,8];
arr.reverse ();
Console.log (arr);
⑤slice (begin,end): [The original array will not be altered] intercepts a portion 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;
⑥sort (): [The original array is changed] the array is sorted 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, or 1 if not found;
lastIndexOf (Value,index): Returns the subscript of the last value value in the array, or 1 if 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;

⑧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 the subscript
(this function is not supported prior to IE8!!!!! )
var arr = [1,5,6,3,4,2,7,6,4,8];
Arr.foreach (function (item,index) {
Console.log (item);
})
⑨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
read 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)

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 the changes will also change.
③ numeric Type, String, Boolean and other variables belong to the basic data type;
an array in which the object belongs to the reference data type;

Built-in objects

Boolean class
There are two ways of declaring it:
You can declare a simple variable using the literal method: Detect with TypeOf as a Boolean type
You can also use the new Boolean () to declare an object of type Boolean. The object type is detected with TypeOf.

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 (); Convert numeric type to String type
Eg:var str = num1.tostring ();
. toLocaleString (); Converts a numeric value to a string in the local format order, usually starting from the right, with three groups separated by commas;

String class
1, attribute: Str.length Returns the length of the string, the number of characters
The string supports an array-like subscript access: str[0];
2. Method:
. toLowerCase (); Convert all characters of a string to lowercase
. toUpperCase (); Turn all characters in a string to uppercase
. CharAt (n); intercept the nth character of an array, quite a str[n]
. IndexOf ("str", index); from the index position, find the substring in the position of the string, if not found return-1, other from the array of indexOf methods;
. LastIndexOf (); same array
. substring (bengin,end); intercept substrings from a string
only one parameter is passed, representing the beginning of begin to the last;
Pass in two parameters, representing the interval between begin and end, left closed right
. Split ("delimiter"), separating the string into the array with the specified delimiter, and passing in an empty "" means that each character of the string is put into an array separately;
. Replace ("old", "new"); replaces the first old in the 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, and if it is a regular expression, it can be substituted according to the wording of the expression.

date Date class
 1, New Date (): Returns the current latest time
 new date ("2017,8,31,12:34:56"); Returns the specified time
 2, Common methods:
Span style= "font-size:16px" > .getfullyear (): Get 4-bit year
 .getmonth (): Get month 0-11
 .getdate (): Gets the day of the one month 1-31
 .getday (): Gets the day of the week 0-6,0 represents Sunday
 .gethours () : Get hours
 .getminutes (): Get minutes
 .getseconds (): Get seconds


Eg:var str = num1.tolocalestring ();
. toFixed (n); leave number n as decimal and convert to string format
Eg:var str = num1.tofixed (2);
. toprecision (n); formats a number as a specified length, n means no decimal digit length, and if n< the original number length, it is represented by scientific notation. If n> the original number length, then 0 after the decimal point;
Eg:var str = num1.toprecision (2);
. valueOf (); Returns the base numeric value of the number object;

"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 an object corresponds to a key name, with a key value.
③ Property: A series of variables that describe the characteristics of an object, called a property. "Variables in an object"
④ Method: A series of functions that describe the behavior of an object, called a method. "Functions in Objects"

2. Declaration of the object :
① literal declaration: Var obj={
key1:value1,
Key2:value2,
func1:function () {}
}
the data in the >>> object is stored as a key-value pair, separated by a key from the value:
for each key-value pair, separate.
A key in the >>> object, which can be any data type except for an array/object. However, it is common to use only ordinary variable names as keys.
the value in the >>> object, which can be any data type, including arrays and objects.
② using the New keyword declaration: var obj=new Object ();
var.key1=value1;
obj.func=function () {}

3. Read and Write properties and methods in the object:

①. Operator: Object name. Property Object name. Method ();
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 contains special characters, only the ② method can be used;
The >>> object directly writes the variable name, 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 can be used, but the This keyword is recommended.
③ Deleting an object's properties and methods: Delete Object name. Property name/Method name
Delete person.age;

JS arrays, built-in objects, custom objects

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.