Built-in objects in JS

Source: Internet
Author: User

The array in JS

1, The basic concept of the array? 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? ① use literal declaration: var arr = [];//declares an empty array "note" in js, the same array can store various data types  ② Use the new keyword declaration: var arr = new Array (parameter);Parameters: A. Omitting the representation of creating an empty array;B. An integer representation declares an array of length for the specified lengths. But this length can be variable at any time, that is, the length of the array can be increased. c. Multiple numeric values separated by a comma, representing multiple value of the Array. new Array (all-in-a--) = = [n/a];3, in the array, the elements read/write/delete? ① Read and write: access elements by subscript, subscript starting from 0. eg. arr[1] = "haha";② Additions and deletions:A. Using the delete keyword, Delete one of the values in the array, the length of the array is unchanged, and the corresponding position becomes Undefined. Eg.delete arr[1]; To delete a value in an arrayB. Arr.pop (); deletes the last value in the Array. Equivalent to arr.length-= 1;difference from delete: array length-1c. Arr.shift (); deletes the first value in the Array. D. Arr.unshift (value); adds a new value at the beginning of the array (No. 0 position). 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. arr[100] = 1; If there is an empty remaining mark in the middle, the undefined will be deposited. 4. Other methods (functions) in the array①join ("delimiter"): Links the array to a string with the specified delimiter, separated by default when the argument is Empty. ②concat (): The original array will not be changed. Link an array to two or more arrays of values as a new array ' "note" If there is a two-dimensional array when the concat is connected, at most one layer is removed;  [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]③arr.push (value): The array is added at the end of one;Arr.unshift (value): adds one at the beginning of the array--returns the length of the new array;arr.pop (): Array finally deletes one;Arr.shift (): Delete 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. Reverses the array and outputs it in Reverse. ⑤arr.slice (begin,end): The original array is not Changed. Intercepts a portion of an array and returns a new array that is Intercepted. Pass in a parameter that represents the start interval, which is truncated to the last array by default;Pass in two parameters, indicating the start and end subscripts, left closed right (contains begin, does not contain End)two parameters can be negative, which means the number starts from the right and the last value is-1⑥sort (): The original array is changed, and the array is sorted in ascending order. 1) by default, the ASCII value of each element is sorted by the first letter of the code;[3,1,5,12].sort (); [1,12,3,5]    2) You can pass in a comparison function, manually specify the sorting function algorithm;the function defaults to receive two values, a, or if the return value >0, a>barr.sort (function (b) {return b-a;//descending orderreturn a-b;//ascending Order});⑦indexof (value,index): Returns the subscript where the first value value in the array is located, if no return-1 is found;lastIndexOf (value,index): If a value exists in more than one case, returns the subscript of the last value in the array, if no return-1 is found;If index is not specified, it indicates that the value is found in the entire arrayIf 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 can receive two parameters,the first parameter is the value of each item in the array, and the second parameter is the subscript for the Array. "not supported before IE8"Arr.foreach (function (item,index) {Console.log (item);});⑨map (): Array mapping. Use the same way as foreach (). Unlike foreach (), a map can have a return value indicating that each value of the original array is manipulated to return a new arrayvar arr0 = arr.map (function (item,index) {//array Mapping returns a new arrayConsole.log (item);return item-1;});⑩.splice (no-1,1); insert, delete, replace (third Argument) an element in an array. 5, two-dimensional arrays and sparse arrays (understanding)① two-dimensional array: The value in the array is still an array form. Eg. arr var a = [[[1,2,3],[3,4,5]];//two rows three columnsreading a two-dimensional array: a[[column number]② Sparse array: The index in the array is not contiguous. (length is greater than the actual number of elements in the Array)basic data types & Reference data types① Basic Data type: When you assign a value, you assign the value in the meta variable to another variable. When the assignment is complete, 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, the same data is accessed, one of the values is modified, and the other Changes. ③ Numeric type, string type, boolean type and other variables belong to the basic data type;an array, an object, belongs to a reference data type. other built-in objects1. Boolean classtwo ways to declare: you can declare a simple variable using literal method;You can also use the new Boolean () to declare an object of type Boolean. The object type is detected with Typeof. 2. Number classCommon Methods:max_value: Returns the maximum value that can be represented by the number class;Min_value: Returns the minimum value that the number class can represent;. ToString (): Converts a numeric type to a string type. (common). toLocaleString (): Converts a numeric value to a string in the local format order.generally starting from the right, three a comma-delimited set of. toFixed (n): Keep numbers n decimal places and convert to string format. (common). toprecision (): Formats the number as a specified length. N-the length of the digits without the decimal point, or the scientific notation if n is less than the original number Length. If n is greater than the original digit length, then the decimal point is 0;. ValueOf (): Returns the base numeric value of the number Object. 3. String Class1) property: Str.length Returns the length of the string that is the number of characters;the string supports an array-like subscript access mechanism: str[0]2). tolocalelowercase (); Convert all characters of a string to lowercase. toLocaleUpperCase (); turn all characters in a string to uppercase3). charAt (n); Intercept the nth character of an array, equivalent to str[n] (common)4). indexOf ("str", index); From the index position, find where the string appears,If no return-1 is found, the other indexof method of the same array. lastIndexOf ("str", index), Same array5). substring (begin,end); To intercept a substring from a stringonly one parameter is passed: the start from begin to the lastPass in two parameters, representing the interval from begin to end, left closed and right Open. 6). split (): separates the strings with the specified delimiter. stored in the Array. incoming null "" means that each character of the string is stored separately in the Array. 7). Replace ("old", "new"); Replaces the first old of a string with NEW. the first argument 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 replaced according to the requirements of the regular Notation. 3.Date Dates class object 1) New Date (): Returns the current latest Time. new Date ("2017,12,31,12:34:12"); returns the specified Time. 2) Common Methods:①.getfullyear (): Gets the year. ②.getmonth (): Gets the Month. ③.getdate (); Get the day of the one month④.getday (); Gets the day of the week 0-represents SundayCustom Objects1. Basic Concepts① Object: A collection that has a series of unordered properties and METHODS. ② Key-value pairs: The data in the object, in the form of a key-value pair. Each property and method of the object corresponds to a key name of the value, with a key Value. ③ attribute: "variable in object"; a series of variables depicting the characteristics of an object ;④ method: "function in object"; a series of functions that describe the behavior of an object. 2. Declaration of Objects① use literal declaration:the data in the "note" object is stored as a key-value pair, separated by the key from the Value: Multiple Key-value pairs, separated by  the key in an object can be any data type other than an array/object, but we generally use the normal variable namethe values in the object can be any data type, including arrays and Objects. var obj = {key1:value1,key2:value2,func1:function () {}}② uses the new keyword declaration:var obj = new Object ();obj.key1:value1;obj.key2:value2;obj.func1:function () {}3. Read and write properties and methods in objects①. operator:Object Inside: This. Property This. method ();external Object: Object Name. Property Object Name. method ();②["key"] Call: Object name ["property name"] object name ["method name]" (); Note : If the key contains special characters, you can only use the ② method.     The name of the variable is written directly in the object, and the global variable is called by Default. If you need to invoke the properties or methods of the object itself, you need to use the object Name. property, or This. Property. this.age person.age The This keyword is recommended. 4. Deleting properties and methods of objectsDelete Object Name. property Name/method name;eg. Delete person.age; Thank you for reading, please give more advice, mutual encouragement.   

Built-in objects in JS

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.