Use JavaScript to create your own objects. Although the functionality within JavaScript and the browser itself is already powerful, JavaScript provides a way to create a new object. So that it does not need to be like Hypertext Markup Language, or other multimedia tools, can do a lot of complex work.
Creating a new object in JavaScript is very simple. First it must define an object and then create an instance for that object. This example is a new object that has the basic characteristics of the object definition.
I. Definition of the object
A JavaScript object is defined as the following basic format:
Function Object (property sheet)
This.prop1=prop1
This.prop2=prop2
...
this.meth=functionname1;
this.meth=functionname2;
...
In the definition of an object, you can specify its properties and methods for that object. An instance of an object is formed by means of properties and methods. As follows is a definition of the University object:
Function University (Name,city,creatdate URL)
This.name=name
This.city=city
This.creatdate=new Date (creatdate)
This.url=url
The basic meaning is as follows:
name-Specifies a "unit" name.
city-"unit" in the city.
creatdate-records the update date of the University object.
Url-the object points to a URL.
Second, create an object instance
Once the object definition is complete, you can create an instance of the object:
Newobject=new object ();
Where Newobjet is a new object, an object that has already been defined. Cases:
U1=new University ("Yunnan province", "Kunming", "January 05,199712:00:00", "Http://www.YN.KM")
U2=new University ("Yunnan", "Kunming", "January 07,1997 12:00:00", "htlp://www.ynkj.cn")
Third, the use of object methods
In addition to using attributes in an object, you sometimes need to use a method. In the definition of an object, we see the This.meth=functionname statement, which is the method for defining the object. The method of the substance object is a function functionname, through which it realizes its own intention.
Example adds a method to the University object that displays itself and returns the corresponding string.
Function University (Name,city,createdate,url)
This.name=name;
this.city=city;
This.createdate=new Date (creatdate);
This.url=url;
this.showuniversity=showuniversity;
Where this.showuniversity is defined as a method---showuniversity ().
The Showuniversity () method is to implement the display of the university object itself.
function showuniversity ()
For (Var prop in this)
Alert (prop+= "+this[prop]+");
Where alert is an intrinsic function in JavaScript, displays its string.