Data types other than null and undefined are defined as objects in JavaScript, and variables can be defined using the method of creating objects, String, Math, Array, Date, RegExp are important built-in objects in JavaScript, and most of the functionality in JavaScript programs is based on object implementations.
One, String object
1. Creation of String objects
// mode one: variable = "string"s= "HEllo World";
// mode Two: string Object name = new String (string)a=new string ("Hello World");
2. String Object Properties and functions
The specific list is as follows:
X.length----get the length of the string X.tolowercase () ---To lowercase x.touppercase ()----to uppercase X.trim () -------String Query method X.charat (index) ----Find the character x.indexof (findstr) by index------Search by character index X.lasti Ndexof (findstr) x.match (regexp)----match returns an array of matching strings and returns if no match is returned nullx.sear CH (regexp)----search Returns an example of the first character position index of a matching string: varStr1= "Welcome to the world of js!"; varStr2=str1.match ("World"); varStr3=str1.search ("World"); Alert (str2[0]);//The result is "world"alert (STR3);//The result is----substring processing method x.substr (start, length)----intercept from index to start The length of the string x.substring (start, end)----intercepts the string from index start to index end, and does not contain the character of the index end X.S Lice (start, end)----example of a slice manipulation string: varstr1= "ABCDEFGH"; varStr2=str1.slice (2,4); varStr3=str1.slice (4); varStr4=str1.slice (2,-1); varStr5=str1.slice ( -3,-1); alert (STR2); //The result is "CD"alert (STR3);//The result is "EFGH"alert (STR4);//The result is "CDEFG"alert (STR5);//The result is "FG"
X.replace (FINDSTR,TOSTR)----string substitution x.split (); ---split stringvarStr1= "One, two, three, four, five, six, day"; varStrarray=str1.split (","); Alert (strarray[1]);//The result is "two"X.concat (ADDSTR)---stitching strings
Second, array Arry object
1. Creation of Array objects
// Create mode 1: var array name = [element 0, Element 1,....]; // such as: Var arr=[1,2,3];
// Create mode 2: var New Array (element 0, Element 1,....); // such as: Var arr=new Array (+, "a", true);
// Create mode 3: var New var arr=New Array (7); arr[0]= "Sunday"; arr[1]= "Monday";.... arr[6]= " Saturday ";
2. Properties and methods of array objects
(1) Join () method
To spell an array into a string:
s=["Hello", "word"]var ret=s.join ("|") ); Console.log (ret); // The result is: Hello|word
(2) Concat () method
To enrich the array, unlike the list extend () method in Python, this method does not alter the original array, but only obtains an expanded array copy.
var arr=[11,3.14,true, "Hello"]; var ret=arr.concat (444,555); As an array form can also: [444,555]console.log (ret )// Result: [One, 3.14, true, "Hello", 444, 555]
(3) Reverse () method
Reverses the existing array, changing the existing data order:
var arr=[12,100,23,45];arr.reverse () console.log (arr); // results: [Max, Max, +] Console.log (arr.tostring ()) // result: 45,23,100,12
(4) Sort () method
Reorder the existing array order according to the order of the ACSSII code:
var arr=[12,100,23,45];arr.sort () console.log (arr); // The results are: [ +, +,]
The above array of numbers is sorted by the true size of the numbers as follows:
var arr=[12,100,23,45]; function Mysort (b) { return A-b }arr.sort (mysort) console.log (arr) ; // [ up to, Max.]
(5) Slice () method
To slice through an array of indexes, as in the case of this method in the string, the example is as follows:
var arr1=[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ']; var arr2=arr1.slice (2,4); var arr3=arr1.slice (4); var arr4=arr1.slice (2,-1); alert (arr2.tostring ()); // alert (arr3.tostring ()); // The result is "e,f,g,h"alert (arr4.tostring ()); // The result is "C,d,e,f,g"
(6) push () and pop () methods
Push is the value that is added to the end of the array x, and value can be any value such as a string, a number, an array, and so on. The pop is to delete the last element of the array x:
vararr3=[111,222,333];arr3.push (444,555); Console.log (ARR3); //[111, 222, 333, 444, 555]Arr3.push ([666,777])//Array [666,777] is placed in the array aar3 as a wholeConsole.log (ARR3)//[111, 222, 333, 444, 555, Array (2)]vars=Arr3.pop (); Console.log (ARR3); //[111, 222, 333, 444, 555]Console.log (s);//[666, 777]
(7) Unshift () and Shift ()
In contrast to (6), unshift () adds value to the beginning of the array x, and value can be any value, such as a string, a number, an array, and so on. Shift is to delete the first element of the array x:
var arr3=[111,222,333];arr3.unshift (555,6666); // multiple characters are placed in the original order as a whole in front of the array console.log (ARR3); // [555, 6666, 111, 222, 333]s=arr3.shift (); Console.log (ARR3) //[6666, 111, 222, 333]Console.log (s) //555
(8) Splice ()
X. Splice (Start, DeleteCount, Value, ...), the main purpose of splice is to delete and insert the specified position of the array, start indicates the starting position index, and deletecount the number of elements to be deleted. Value represents an array element that is inserted at the drop location, which can be omitted.
var a = [1,2,3,4,5,6,7,8];a.splice]; alert (a.tostring ()); // a changes to [1,4,5,6,7,8]A.splice (N); alert (a.tostring ()) ; // a becomes [1,5,6,7,8]a.splice (1,0,2,3); alert (a.tostring ()); // a changes to [1,2,3,5,6,7,8]
The properties of arrays in JS: The properties of arrays in Java, what type of arrays are specified, and only what types are loaded. There is only one type. The array attribute in JS 1:js can be any type, without any restriction; the array in the 2:JS of the array attribute in JS, the length is changed with the subscript. How long it will take.
Third, data time object
1. Create data Object
//Method 1: Do not specify a parameter that represents the current timevarnowd1=NewDate (); alert (nowd1.tolocalestring ()); //2017/11/9 pm 5:20:54//Method 2: The parameter is a date stringvarNowd2=NewDate ("2004/3/20 11:12"); alert (nowd2.tolocalestring ()); //The result is:2004/3/20 Morning 11:12:00varnowd3=NewDate ("04/03/20 11:12"); alert (nowd3.tolocalestring ()); //The result is:2020/4/3 Morning 11:12:00//Method 3: The parameter is of millisecondsvarnowd3=NewDate (5000); alert (nowd3.tolocalestring ()); //The result is:1970/1/1 Morning 8:00:05Alert (nowd3.toutcstring ());//The result is:Thu, 1970 00:00:05 GMT//Method 4: Parameter is month day hour minute seconds millisecondvarnowd4=NewDate (2004,2,20,11,12,0,300); alert (nowd4.tolocalestring ()); //The result is:2004/3/20 11:12:00 milliseconds does not show directly
2. Get the date and time
Gets the date and time getdate () gets the day getday () Gets the week getmonth () Gets the month (0-11) getfullyear () Gets the full year getyear ( Gets the Year gethours () Gets the Hour getminutes () Gets the Minute getseconds () Gets the Seconds getmilliseconds () Gets the milliseconds gettime () returns the cumulative number of milliseconds (from 1970/1/1 Midnight)
Application Example: Output the current time, such as: "June 18, 2014 15:40:30 Wednesday"
functiongetcurrentdate () {varDate =NewDate ();//1. Create a Date object without filling in any parameters so that's the current time. varYear = Date.getfullyear ();//2. Get the current year varmonth = Date.getmonth () +1;//3. Get the current month JS in the month is from 0 to one. varDay = Date.getdate ();//4. Get the current day varhour = Date.gethours ();//5. Get the current hour varmin = Date.getminutes ();//6. Get the current minute varSEC = Date.getseconds ();//7. Get Current seconds varWeek = Date.getday ();//8. Get the current week, no Getweek returnyear+ "Year" +changenum (month) + "monthly" +day+ "Day" +hour+ ":" +min+ ":" +sec+ "" +Parseweek (week); }alert (Getcurrentdate ());//solutions for automatically completing two-digit numbers functionchangenum (num) {if(Num < 10){ return"0" +num; }Else{ returnnum; }}//convert digital 0~6 to Sunday to Saturday functionParseweek (week) {vararr = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; //0 1 2 3 ... .... returnArr[week];}
Python Learning _day54_ front-end Basics JS (2)