JavaScript Learning Notes JS Object _ Basic Knowledge

Source: Internet
Author: User
Tags array length arrays pear javascript array

Default Object

Date Object,

Format: Date Object name =new date ([date parameter])

Date Parameters:

1. Ellipsis (most commonly used);

2. English-numeric format: month, year [Time: minutes: sec]

such as: Today=new Date ("October 1,2008 12:00:00")

3. Numerical format: Ad year, month, day, [Time, minute, second]

such as: Today=new Date (2008,10,1)

Method of Date Object:

Format: Date Object name. Method ([parameters])

Use examples:

Copy Code code as follows:

<body>
<script type= "Text/javascript" >
var date = new Date (), default provided object in//js
Document.writeln ("Now Moment:" + (Date.getyear () + 1900) + "Year"
+ (Date.getmonth () + 1) + "month" + date.getdate ()
+ "Day" + ", Week" + date.getday () + ", Time:"//Sunday will be 0, need further processing, here first not to deal with
+ date.gethours () + ":" + date.getminutes () + ":" + date.getseconds ());
</script>
</body>

Output:

Now moment: April 21, 2014, Week 1, Time: 14:7:53

Array Objects
the function of an array object is to store a series of values using a separate variable name.

The JavaScript array has two points of specificity:

1. Array length is indefinite, can enlarge automatically;

2. The data types stored in the array can be not uniform, that is, you can mix different data types.

Create multiple formats for an array object:

New Array ();

The returned array is empty and the length field is 0.

New Array (size);

The parameter size is the number of expected array elements. The returned array, the Length field will be set to the value of size. The constructor returns an array with the specified number of elements undefined.

New Array (Element0, Element1, ..., ELEMENTN);

The constructor initializes the array with the value specified by the parameter, and the length field of the array is set to the number of parameters.

Array object name =[element 1[, Element 2, ...]

(Note that square brackets are used here).

When you call a constructor as a function, and you do not use the new operator, it behaves exactly as it did when you called it with the new operator.

You can also create two-dimensional arrays.

The method of the array object can be seen in: http://www.w3school.com.cn/jsref/jsref_obj_array.asp

Use an instance of an array object:

Copy Code code as follows:

<! DOCTYPE html>
<title>arrayTest.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<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/>");
}
Some methods of array test
With (document)
{
Write ("<ul>");
Write ("<li>" + fruits.join () + "</li>");//By default use commas to separate
Write ("<li>" + fruits.join (";") + "</li>");
Write ("<li>" + fruits.tostring () + "</li>");
Write ("<li>" + fruits.reverse (). Join () + "</li>");
Write ("<li>" + fruits.valueof () + "</li>");
Stating that the above reverse is actually changing 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 mixed with
Traversing two-dimensional arrays
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>

String Object
To create a String object:

Format: String Object name =new string (string constant)

Format: string Variable name = "String Constant"

An example of verifying an email:

Copy Code code as follows:

<! DOCTYPE html>
<title>emailConfirm.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are 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>
<body>
<form>
Email: <input type= "text" name= "email" ><br/>
<input type= "button" value= "Check" onclick= "Isemail ()" >
</form>
</body>

Custom Objects
Before we talk about the function, I'll give you an example, and here's another example:

Copy Code code as follows:

<! DOCTYPE html>
<title>objectTest.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<body>
<script type= "Text/javascript" >
One way to define an object: through a constructor
function member (name, gender)
{
Property
THIS.name = name;
This.gender = gender;
Method
This.display = display;//The display method for the specified member object
}
function display ()
{
var str = this.name + ":" + this.gender;
This display method is used by WHO, this here is pointing to that object
Document.writeln (str + "<br/>");
}
Build 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 ("Invoke Method", "<br/>");
M1.display ();
M2.display ();
M3.display ();
M4.display ();
</script>
</body>

Small partners have a new understanding of the concept and usage of JavaScript objects, I hope you can enjoy this article and this series of articles.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.