SoFunction
Updated on 2025-04-06

JavaScript learning notes: JS object

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, 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:

Copy the codeThe code is as follows:

<body>
    <script type="text/javascript">
var date = new Date();//Objects provided by default in JS
("Current moment: " + ( () + 1900 ) + "year"
+ (() + 1) + "month" + ()
+ "Sun" + ", Week" + () + ", Time: "//Sunday will be 0, and further processing is required. It will not be processed here.
                + () + ":" + () + ":" + ());
    </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 element is 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.

For the method of Array object, please refer to:http:///jsref/jsref_obj_array.asp

Examples of array object usage:

Copy the codeThe code is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title></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="./">-->
  </head>
  <body>
    <script type="text/javascript">
    //var fruits = new Array("Apple", "Banana", "Pear");
var fruits = ["Apple","banana","pear"];//Recommended use
//Elements can be added dynamically
("watermelon");
("orange");
    for(var i = 0; i < ; ++i)
    {
        ("fruit[" + i + "] = " + fruits[i] + "<br/>");
    }
//Some method tests of arrays
    with(document)
    {
        write("<ul>");
write("<li>" + ()+ "</li>");//Use commas to separate by default
        write("<li>" + (";")+ "</li>");
        write("<li>" + ()+ "</li>");
        write("<li>" + ().join()+ "</li>");
        write("<li>" + ()+ "</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 through two-dimensional arrays
    for(var i = 0 ; i < ; ++i)
    {
        for(var j= 0 ; j < people[i].length ; ++j)
        {
            ("people["+ i +"]["+ j +"] = " + people[i][j] + "<br/>");
        }
        ("<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:

Copy the codeThe code is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title></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="./">-->
    <script type="text/javascript">
        function isEmail()
        {
            var emailValue = ("email")[0].value;
            if(-1 == ("@"))
            {
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:

Copy the codeThe code is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title></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="./">-->
  </head>
  <body>
    <script type="text/javascript">
//A way to define objects: by constructing functions
        function member(name, gender)
        {
//Properties
            = name;
            = gender;
//method
= display;//Specify the display method of the member object
        }
        function display()
        {
            var str = + " : " + ;
//Who has used this display method? This here points to that object
            (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( + ":" + + "<br/>");
            write( + ":" + + "<br/>");
            write( + ":" + + "<br/>");
            write( + ":" + + "<br/>");
        }
("Call method","<br/>");
        ();
        ();
        ();
        ();
    </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.