Default object
Date object Date,
Format: Date object name = new Date([Date Parameter])
Date parameters:
1. Omit (most commonly used);
2. English-numerical format: month and day, year 1 [hour: minute: seconds]
For example: today=new Date("October 1,2008 12:00:00")
3. Numerical format: First year, month, day, [hour, minute, second]
For example: today=new Date(2008,10,1)
Methods of date objects:
Format: Date object name. Method ([parameter])
Example of use:
The code copy is as follows:
<body>
<script type="text/javascript">
var date = new Date();//Objects provided by default in JS
document.writeln("present moment: " + ( date.getYear() + 1900 ) + "year"
+ (date.getMonth() + 1) + "month" + date.getDate()
+ "Day" + ", Weekly" + date.getDay() + ", Time: "//Sunday will be 0, and further processing is required, so it will not be processed here.
+ date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
</script>
</body>
Output:
Current Moment: April 21, 2014, 1, Time: 14:7:53
Array Objects
The function of an array object is to use a separate variable name to store a series of values.
JavaScript arrays have two special features:
1. The array length is uncertain and can be automatically expanded;
2. The data types stored in the array may be inconsistent, that is, different data types can be stored in mixed storage.
Create multiple formats for array objects:
new Array();
The returned array is empty and the length field is 0.
new Array(size);
The parameter size is the expected number of array elements. The returned array, the length field will be set to the value of size. This constructor returns an array with the specified number and elements are undefined.
new Array(element0, element1, ..., elementn);
This constructor will initialize the array with the value specified by the parameter, and the length field of the array will be set to the number of parameters.
Array object name = [Element 1[, Element 2,...]]
(Note that square brackets are used here).
When a constructor is called as a function without using the new operator, its behavior is exactly the same as when it is called with the new operator.
You can also create two-dimensional arrays.
The method of Array object can be found at: http://www.w3school.com.cn/jsref/jsref_obj_array.asp
Examples of array object usage:
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>arrayTest.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<script type="text/javascript">
//var fruits = new Array("Apple", "Banana", "Pear");
var fruits = ["Apple","banana","pear"];//Recommended use
//You can add elements dynamically
fruits.push("watermelon");
fruits.push("orange");
for(var i = 0; i < fruits.length; ++i)
{
document.writeln("fruit[" + i + "] = " + fruits[i] + "<br/>");
}
//Array method test
with(document)
{
write("<ul>");
write("<li>" + fruits.join()+ "</li>");//Default is used to separate it with commas
write("<li>" + fruits.join(";")+ "</li>");
write("<li>" + fruits.toString()+ "</li>");
write("<li>" + fruits.reverse().join()+ "</li>");
write("<li>" + fruits.valueOf()+ "</li>");
//Indicate that the reverse above actually changed the array itself
write("</ul>");
}
//Two-dimensional array
var people = new Array(3);
people[0] = new Array(1, "zhangsan", "lisi");
people[1] = new Array(2, "Jack", "Lucy");
people[2] = new Array(3, "Xiaoming", "Xiaohong");
//Note that data types can be used in a mixed manner
//Transfer the two-dimensional array
for(var i = 0 ; i < people.length ; ++i)
{
for(var j= 0 ; j < people[i].length ; ++j)
{
document.write("people["+ i +"]["+ j +"] = " + people[i][j] + "<br/>");
}
document.write("<br/>");
}
</script>
</body>
</html>
String Object
Create a string object:
Format: String object name = new String (String constant)
Format: String variable name="String constant"
An example of verifying email:
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>emailConfirm.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function isEmail()
{
var emailValue = document.getElementsByName("email")[0].value;
if(-1 == emailValue.indexOf("@"))
{
alert("Please fill in the correct email address");
}
else
{
alert("Ok");
}
}
</script>
</head>
<body>
<form>
email: <input type="text" name="email"><br/>
<input type="button" value="check" onclick="isEmail()">
</form>
</body>
</html>
Custom Objects
I mentioned an example when I talked about functions before. Now let me talk about this example:
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>objectTest.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<script type="text/javascript">
//A way to define objects: by constructing functions
function member(name, gender)
{
//property
this.name = name;
this.gender = gender;
//method
this.display = display;//Specify the display method of the member object
}
function display()
{
var str = this.name + " : " + this.gender;
//Who has used this display method? This here points to that object
document.writeln(str + "<br/>");
}
//Generate object
var m1 = new member("zhangsan", "male");
var m2 = new member("lisi", "male");
var m3 = new member("wangwu", "male");
var m4 = new member("wangfang", "female");
with(document)
{
write("Output Properties","<br/>");
write(m1.name + ":" + m1.gender + "<br/>");
write(m2.name + ":" + m2.gender + "<br/>");
write(m3.name + ":" + m3.gender + "<br/>");
write(m4.name + ":" + m4.gender + "<br/>");
}
document.write("Call method","<br/>");
m1.display();
m2.display();
m3.display();
m4.display();
</script>
</body>
</html>
Have you gained a new understanding of the concept and usage of objects in JavaScript? I hope you can like this article and this series of articles.