1. Type of Object
(1) Creating an object instance
The first way: The new operator is followed by the object constructor
var person=new Object ();
Person.name= "Nicholas";
person.age=29;
Second approach: Object literal notation (simplifying the process of creating an object containing a large number of attributes)
var person={
Name: "Nicholas",
Age:29
}
var person={} equals the var person=new Object ();
Object constructors are not actually called when objects are defined through object literals, and are recommended only when considering the readability of object property names;
The object literal is also the preferred way to pass a large number of optional parameters to the function; see program below
function DisplayInfo (args)
{
var output= "";
if (typeof args.name== "string")
{
output+= "Name:" +args.name+ "\ n";
}
if (typeof args.age== "number")
{
output+= "Age:" +args.age+ "\ n";
}
alert (output);
}
DisplayInfo ({
Name: "Nicholas",
Age:29
});
DisplayInfo ({
Name: "Greg",
});
(1) Accessing object properties
In addition to using dot notation, you can use square brackets to access the properties of an object, and the property to be accessed is placed in square brackets in the form of a string, with the advantage that the property can be accessed through a variable;
Var propertyname= "name";
Alert (Person[propertyname]);
You can also use square brackets if the property name contains a character that causes a syntax error, or if the property name uses a keyword or reserved word;
2. Date
(1) Create a Date object: var now=new date (); //object gets the current date and time automatically;
Create a specified date: Date.parse () and DATE.UTC ();
Date.parse () receives a string parameter that represents a date, attempts to return the number of milliseconds from the corresponding date based on the string, and does not define which date format it supports, depending on the implementation and varies by region;
var somedate=new date (Date.parse ("may 25,2004"));//equal to Var somedate=new date ("may 25,2004");
Returns Nan if the passed-in string cannot represent a date
DATE.UTC () function:
Ar y2k=new Date (DATE.UTC (2000,0)); Gmt:2000-1-1
var allfives=new Date (DATE.UTC (2005,4,5,17,55,55))//gmt:2005-5-5 17:55:55
var y2k=new Date (2000,0); local time: 2000-1-1
var allfives=new Date (2005,4,5,17,55,5)//local time: 2005-5-5 17:55:55
The Date.now () method returns the date and time when the method was called, the browser that does not support this method, the use of the + operator to convert the Date object to a string, can achieve the same purpose;
(2) Method of date formatting
toDateString ()-Displays days of the week, month, day and year in a specific implementation format
toTimeString ()-Displays seconds, minutes, and time zones in a specific implementation format;
Tolocaldatestring ()-Displays the week, month, day and year in a specific region format;
Tolocaltimestring ()-Displays the minutes and minutes in the format of a particular region;
toUTCString ()-full UTC date in a format specific to the implementation;
JavaScript reading notes (4)-object Date