In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "built-in object instance analysis in JavaScript". In daily operation, I believe that many people have doubts about the problem of built-in object instance analysis in JavaScript. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "built-in object instance analysis in JavaScript". Next, please follow the editor to study!
I. the concept of object
Object is a special data type (object).
Has a series of properties and methods.
It is divided into built-in objects and custom objects.
2. String object
The String object is used to support string processing.
1. Attributes
Length: gets the length of the string object.
For example:
String object onload=function () {var btn=document.querySelector ("input [type = 'button']"); var len=document.querySelector ("input [type =' text']"); btn.onclick=function () {alert ("string length:" + len.value.length);};}
Effect:
When entering content in the text box:
When there is no input in the text box:
2. Methods
Syntax: string object. Method name ()
For example:
String object onload=function () {var btn=document.querySelector ("input [type = 'button']"); var len=document.querySelector ("input [type =' text']"); var btnMethod=document.querySelector ("input [method']"); var writeDiv=document.querySelector ("# printMethod"); / / var str=len.value.toString () Btn.onclick=function () {alert ("string length:" + len.value.length);}; / / method btnMethod.onclick=function () {var str= "a good man is a real man"; / / 1, CharAt returns the character var charAt=str.charAt (5) at the specified position in the string / / 2. IndexOf looks for the first occurrence of a specified string in the string / / returns the position in the string if it can be found, and returns-1 var str1=str.indexOf ("m"); var str2=str.indexOf ("woman") if the query cannot find it. / / returns-1 / / finds the position where man first appears in a string after the subscript 8 ("man", 8). / / 3, substring (index1,index2) returns the string between the specified index index1 and index2, / / and includes the characters corresponding to the index index1, excluding the characters corresponding to the index index2. Var substr=str.substring (3 str 7); / / returns a string with an index from 3 to 7. Omitting the second parameter indicates var substr2=str.substring (6) up to the end of the string; / / 4, split (str) splits the string into an array of strings var strs=str.split (") / / use "|" to split the array / / 5, toLowerCase () to convert the string to lowercase letters var lowerStr=str.toLowerCase (); / / 6, toUpperCase () to convert the string to uppercase letters var upperStr=str.toUpperCase (); string with var strTemp= "position 5:" + charAt+ "" The position of strTemp+= "character m:" + str1+ ""; the position of strTemp+= "string woman:" + str2+ ""; the location of man after index 8 of strTemp+=: "+ str3+"; the string between index 3 and 7 of strTemp+= is: "+ substr+"; the string after index 6 of strTemp+= is: "+ substr2+" StrTemp+= "array:" + strs+ "; strTemp+=" lowercase: "+ lowerStr+"; strTemp+=" uppercase: "+ upperStr+"; writeDiv [XSS _ clean] = strTemp;};}
Results:
3. Math object
The Math object, which is used to perform common mathematical tasks, contains several numeric constant lights and functions.
The common methods are as follows:
For example:
Math object onload=function () {var btn=document.querySelector ("input [name = 'btnMath']"); var showDiv=document.querySelector ("# show"); btn.onclick=function () {/ / rounding off the decimal part, adding 1 var str= "25.8 rounding value to the integer part:" + Math.ceil (25.8) + "" Rounding value for str+= "25.1:" + Math.ceil (25.1) + "; rounding value for str+="-25.8: "+ Math.ceil (- 25.8) +"; rounding value for str+= "- 25.1:" + Math.ceil (- 25.1) + "" / / the decimal part is rounded off, the rounding value of str+= "25.8 is unchanged:" + Math.floor (25.8) + "; the rounding value of str+=" 25.1 is "+ Math.floor (25.1) +"; the rounding value of str+="-25.8 is "+ Math.floor (- 25.8) +" Rounding value of str+= "- 25.1:" + Math.floor (- 25.1) + "; / / rounding str+=" 25.8: "+ Math.round (25.8) +"; rounding str+= "25.1:" + Math.round (25.1) + "" The rounding value of str+= "- 25.8:" + Math.round (- 25.8) + "; the rounding value of str+="-25.1: "+ Math.round (- 25.1) +"; / / the random number returns the random number between 01,100 random numbers: "+ Math.floor (Math.random () * 100) +". ShowDiv [XSS _ clean] = str;};}
Results:
4. Date object
The Date object is used to manipulate the date and time.
1. Syntax: var myDate=new Date ([parameters])
For example:
2. Methods:
For example:
Date object onload=function () {/ / create Date object var date=new Date (); var btn=document.querySelector ("input [name = 'btnDate']"); var showDiv=document.querySelector ("# show"); btn.onclick=function () {var str= "current date:" + date.getDate () + " Str+= "Today is the week:" + date.getDay () + "; str+=" hours: "+ date.getHours () +"; str+= "minutes:" + date.getMinutes () + "; str+=" seconds: "+ date.getSeconds () +"; str+= "current month:" + date.getMonth () + "" Str+= "current year:" + date.getFullYear () + "; str+=" milliseconds: "+ date.getTime () +"; showDiv [XSS _ clean] = str;};}
Results:
Sample program:
Achieve the clock effect:
Clock onload=function () {var btnStart=document.querySelector ("# btnStart"); var btnStop=document.querySelector ("# btnStop"); var showDiv=document.querySelector ("# show"); var handle; function showTime () {var currentDate=new Date (); var year=currentDate.getFullYear () / / month var month=currentDate.getMonth () + 1; / / Day var day=currentDate.getDate (); / / hour var hour=currentDate.getHours (); / / minute var mit=currentDate.getMinutes () / / second var second=currentDate.getSeconds (); / / week var week; switch (currentDate.getDay ()) {case 0: week= "Sunday"; break Case 1: week= "Monday"; break; case 2: week= "Tuesday"; break; case 3: week= "Wednesday"; break Case 4: week= "Thursday"; break; case 5: week= "Friday"; break; case 6: week= "Saturday"; break ShowDiv [XSS _ clean] = "current time: + year+" year + month+ "month" + day+ "day" + "" + hour+ ":" + mit+ ":" + second+ "" + week;}; btnStart.onclick=function () {showTime (); handle= setInterval (showTime,1000) } btnStop.onclick=function () {clearInterval (handle);}}; at this point, the study on "instance analysis of built-in objects in JavaScript" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.