JavaScript Basic Learning (ii) Instanceof method:
vars = "Hello"; vari = 8; //typeof can only judge basic data typesAlerttypeof(s)); Alert (typeof(i)); //for reference data types, use the instanceof varS2=NewString ("Hello2") Alert (typeof(S2)); Alert (S2instanceofString);//true varn =NewNumber (2); Alert (typeof(n)); Alert (ninstanceofnumber);//true
String Object
To automatically create a string object:
var str1= "Hello World";
To create a string object manually:
var New String ("Hello word");
String property and Method:
<script>vars = "HELlo"; //the property of the string object is lengthalert (s.length); //Traversing Strings for(varIinchs) {Console.log (s[i])}//the repetition will be put together //Orchestration Methoddocument.write (S.italics ());//returns the italic stringdocument.write (S.bold ());//returns a bold representation of a stringdocument.write (S.anchor ("Alex"));//return anchor definition string <a>x</a> //uppercase and lowercase conversionsConsole.log (S.touppercase ());//HELLOConsole.log (S.tolowercase ());//Hello //gets the specified characterConsole.log (S.charat (3))//LConsole.log (S.charcodeat (3))//108 ASC Code value //query stringConsole.log (S.search ("L"));//returns the index value of the first matching resultConsole.log (S.match ("L") [0]);//returns an array with all matching resultsConsole.log (S.match ("L") [1]); //Replace Concat splitConsole.log (S.replace ("E", "E")); Console.log (S.split (E)); Console.log (S.concat ("Wordl")); //Intercept StringConsole.log (S.SUBSTR);//El is taken by numberConsole.log (s.substring);//E by indexConsole.log (S.slice (1,-1));//ELlConsole.log (S.indexof ("L"));//from the trip to the post-fetchConsole.log (S.lastindexof ("L"));//take index values from the back</script>
Split string
var str1= "One, two, three, four, five, six, day"var strarray=str1.split (","); alert (strarray[1]); // results for "two"
Connection String
var str1= "ABCD"var str2=str1.concat ("EFGH"); alert (str2); // The result is "ABCDEFGH"
Array Object
Create mode 1: var a=[1,2,3]; creation Mode 2:new Array (); // allows you to specify the number of elements or specify the number of elements when creating an array. New Array (size); // if 1 parameters and is a number, that represents Size,not content Initialize the array object: var cnweek=New Array (7);
the properties of the array object
Gets the number of array elements:length
Methods for array objects
Connection Array-join method
var arr1=[1, 2, 3, 4, 5, 6, 7]; var str1=arr1.join ("-"); alert (str1); //
Access Stack Operation
var arr1=[1,2,3];arr1.push (4,5); alert (arr1); // The result is "1,2,3,4,5"arr1.push ([6,7]); alert (arr1)// result = "1,2,3,4,5,6,7" Arr1.pop (); alert (arr1); // The result is "1,2,3,4,5"
Date Object
//Method 1: Do not specify parametersvarnowd1=NewDate (); alert (nowd1.tolocalestring ());//Method 2: The parameter is a date stringvarNowd2=NewDate ("2004/3/20 11:12"); alert (nowd2.tolocalestring ());varnowd3=NewDate ("04/03/20 11:12"); alert (nowd3.tolocalestring ());//Method 3: The parameter is of millisecondsvarnowd3=NewDate (5000alert (nowd3.tolocalestring ()); alert (nowd3.toutcstring ()) ;//Method 4: Parameter is month day hour minute seconds millisecondvarnowd4=NewDate (2004,2,20,11,12,0,300); alert (nowd4.tolocalestring ());//milliseconds do not show directly
method of the Date object-Gets 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)
Set the date and time
// set date and time setdate (day_of_month) Set day // Span style= "COLOR: #008000" >setmonth (month) set monthly // setfullyear (year)" Set // // setminutes (min UTE) Set minutes // setseconds (second) set seconds // setmillliseconds (ms) set milliseconds (0-999) // settime (allms) set cumulative milliseconds (from midnight 1970/1/1)
method of the Date object-conversion of date and time
Conversion of date and time: getTimezoneOffset (): 8 time zones x15 degrees X4 min ./degrees = 480; returns the local time difference from GMT, in minutes toutcstring () returns the international Standard Time string tolocalstring () returns the local format time string date.parse (x) returns the cumulative number of milliseconds (from 1970/1/ 1 midnight to local time) DATE.UTC (x) returns the cumulative number of milliseconds (from 1970/1/1 midnight to International time)
Practice
//Customize how the date time is displayed: functiongetcurrentdate () {varDate=NewDate (); varYear=date.getfullyear (); varmonth =Date.getmonth (); varDay =date.getdate (); varhour =date.gethours (); varMin =date.getminutes (); varSEC =date.getseconds (); varWeek =Date.getday (); returnYear + "Years" + changenum (month) + "Month" + Day + "days" + Hour + ":" + min + ":" + sec + "+Parse_week (week); } functionchangenum (num) {if(Num < 10){ return"0" +num; }Else {returnnum}; } functionParse_week (week) {varWeek_arr = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; returnWeek_arr[week]; } alert (Getcurrentdate ())
RegExp Object
//----------------------------How to create 1 /*var reg1 = new RegExp ("^[a-za-z][a-za-z0-9_]{5,11}$", "G"); Verify string var str = "bc123"; Alert (Reg1.test (str));//TRUE//----------------------------creation Method 2/fill in regular expression/match mode; var reg2 =/^[a-za-z][a-za-z0-9_]{5,11}$/g; Alert (Reg2.test (str));//True*/ //-------------------------------The method of the regular object------------------- //The test method ==> tests whether a string is a compound regular rule. The return value is true and false. //-------------------------4 methods that are combined with a regular in a String------------------. //macth Search Split replace varstr = "Hello World"; //alert (Str.match (/o/g));//Find the contents of the compound regular in the string. //alert (Str.search (/h/g));//0 Find the content location in the string that matches the regular expression //alert (Str.split (/o/g));//The string is cut according to a regular expression. Returns an array;Alert (Str.replace (/o/g, "s"));//Hells Wsrld the string as a regular replacement.
var New REGEXP ("\d+", "G"var str= "Abc345segd"; alert (Reg1.test (str)); Alert(Str.match (/\d+/ g)); Alert (Str.search (/\d+/g)),alert (str.split (/\d+/g)), Alert (Str.replace (/\d+/g, "AAA") )
Math Object
// obtaining a random number 0~1 does not include 1. // Rounding // exercise: Get 1-100 of random integers, including 1 and var num=math.random (); Num=num*100; num=math.round (num); alert (num)
ABS (x) the absolute value of the return number. EXP (x) returns the exponent of E. Floor (x) is rounded down with a logarithmic. Log (x) returns the natural logarithm of the number (bottom is e). Max (x, y) returns the highest value in X and Y. Min (x, y) returns the lowest value in X and Y. The POW (x, y) returns the Y power of X. Random number ( )between 0 and 1. Round (x) rounds the number to the nearest integer. Sin (x) returns the sine of the number. sqrt (x) returns the square root of the number. Tan (x) returns the tangent of the angle.
JavaScript Basic Learning (ii)