Home  >  Article  >  Web Front-end  >  JavaScript object

JavaScript object

高洛峰
高洛峰Original
2016-11-25 13:42:51941browse

We’re going to start learning about the built-in JavaScript objects and how to use them. Starting from the next section, we will explain these built-in JavaScript objects in detail.
1. String object
Example 1: Calculate string length







                                                                            body>

< ;script type="text/javascript">

var txt="Hello World!"

document.write("

Big: " + txt.big() + "

")
document.write("

Small: " + txt.small() + "

")

document.write("

Bold: " + txt.bold() + "

")
document.write("

Italic: " + txt.italics() + "

")

document.write("

Blink: " + txt.blink() + " (does not work in IE)

")
document.write("

Fixed: " + txt.fixed() + "

")
document.write("

Strike: " + txt.strike() + "

")

document.write("

Fontcolor: " + txt.fontcolor("Red") + "

")
document.write("

Fontsize: " + txt.fontsize(16) + "

")

document.write("

Lowercase : " + txt.toLowerCase() + "

")
document.write("

Uppercase: " + txt.toUpperCase() + "

")

document.write ("

Subscript: " + txt.sub() + "

")
document.write("

Superscript: " + txt.sup() + "

")

document.write("

Link: " + txt.link("http://www.w3school.com.cn") + "

")





indexOf() method
How to use indexOf() to locate the first occurrence of a specified character in a string.
match() method
How to use match() to find a specific character in a string, and if found, return that character.







How to replace characters in a string - replace()
How to use the replace() method to replace certain characters with other characters in a string.










2. JavaScript Date (Date) Object
Define Date
Date object is used to handle date and time.
Date objects can be defined through the new keyword. The following code defines a Date object named myDate:
var myDate=new Date()
Note: The Date object automatically uses the current date and time as its initial value.
Operating Dates
By using methods on date objects, we can easily operate on dates.
In the following example, we set a specific date (August 9, 2008) for the date object:
var myDate=new Date()
myDate.setFullYear(2008,7,9) 
注意:表示月份的参数介于 0 到 11 之间。也就是说,如果希望把月设置为 8 月,则参数应该是 7。 
在下面的例子中,我们将日期对象设置为 5 天后的日期: 
var myDate=new Date() 
myDate.setDate(myDate.getDate()+5) 
注意:如果增加天数会改变月份或者年份,那么日期对象会自动完成这种转换。 
比较日期 
日期对象也可用于比较两个日期。 
下面的代码将当前日期与 2008 年 8 月 9 日做了比较: 
var myDate=new Date(); 
myDate.setFullYear(2008,7,9); 
 
var today = new Date(); 
 
if (myDate>today) 

alert("Today is before 9th August 2008"); 

else 

alert("Today is after 9th August 2008"); 

 
 
getTime() 
getTime() 返回从 1970 年 1 月 1 日至今的毫秒数。 
 
 
 
 
 
 
 
setFullYear() 
如何使用 setFullYear() 设置具体的日期。 
 
 
 
 
 
 
 
toUTCString() 
如何使用 toUTCString() 将当日的日期(根据 UTC)转换为字符串。 
 
 
 
 
 
 
 
getDay() 
如何使用 getDay() 和数组来显示星期,而不仅仅是数字。 
 
 
 
 
 
 
 
显示一个钟表 
如何在网页上显示一个钟表。 
 
 
 
 
 
 




3. JavaScript Array (array) object
Merge two arrays - concat()
How to use the concat() method to merge two arrays.







Use the elements of the array to form a string - join()
How to use the join() method Concatenates all elements of the array into a string.







Literal Array - sort()
How to use the sort() method to literally sort an array.







Number array - sort()
How to use the sort() method Sort an array numerically.

6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[ 5] = "1"

document.write(arr + "
")
document.write(arr.sort(sortNumber))







4. JavaScript Boolean (logical) object
Create a Boolean object
Use the keyword new to define a Boolean object. The following code defines a logical object named myBoolean:
var myBoolean=new Boolean()

5. JavaScript Math (arithmetic) object
round()
How to use round().
random()
How to use random() to return a random number between 0 and 1.
max()
How to use max() to return the larger of two given numbers.
min()
How to use min() to return the smaller of two given numbers.

6. JavaScript RegExp Object
Definition of RegExp
RegExp object is used to store retrieval patterns.
Use the new keyword to define the RegExp object. The following code defines a RegExp object named patt1, whose mode is "e":
var patt1=new RegExp("e");
When you use this RegExp object to search within a string, you are looking for characters "e".
Methods of the RegExp object
The RegExp object has 3 methods: test(), exec() and compile().
test()
test() method retrieves a specified value in a string. The return value is true or false.
Example:
var patt1=new RegExp("e");

document.write(patt1.test("The best things in life are free"));
Due to the presence of the letter "e" in the string, the above The output of the code will be:
true
TIY
exec()
exec() method retrieves the specified value in the string. The return value is the found value. If no match is found, null is returned.
Example 1:
var patt1=new RegExp("e");

document.write(patt1.exec("The best things in life are free"));
Since the letter "e" exists in the string, the output of the above code will be:
e
TIY
compile()
compile( ) method is used to change RegExp.
compile() can not only change the retrieval mode, but also add or remove the second parameter.
Example:
var patt1=new RegExp("e");

document.write(patt1.test("The best things in life are free"));

patt1.compile("d");

document.write(patt1.test("The best things in life are free"));
Since "e" exists in the string but not "d", the output of the above code is:
truefalse


7. JavaScript HTML DOM Objects
• Previous Page
• Next Page
In addition to the built-in JavaScript objects, you can also use JavaScript to access and process all HTML DOM objects.
More JavaScript Objects
Click the links below to get more information about the following objects, including their collections, properties, methods, and events.
Object Description
Window
The top-level object in the JavaScript hierarchy. The Window object represents the browser window. Whenever a or tag appears, a Window object is automatically created.
Navigator
Contains information about the client browser.
Screen
Contains information about the client screen.
History
Contains the URLs visited by the browser window.
Location
contains information about the current URL.
HTML DOM
HTML DOM is a W3C standard (the abbreviation of HTML Document Object Model, Document Object Model for HTML).
HTML DOM defines a set of standard objects for HTML, as well as standard methods for accessing and processing HTML documents.
Through the DOM, you can access all HTML elements, along with the text and attributes they contain. The content can be modified and deleted, and new elements can also be created.
HTML DOM is platform and programming language independent. It can be used by any programming language such as Java, JavaScript and VBScript.
Click on the link below to get more knowledge on how to use JavaScript to access and manipulate DOM objects:
Object Description
Document
Represents the entire HTML document and is used to access all elements in the page.
Anchor
represents the element.
Area
represents the element in the image map.
Base
represents the element.
Body
represents the element in the image map.
Button
represents the