Use JavaScript to create your own objects. Although the function of JavaScript and browser itself is very powerful, JavaScript provides a method of creating a new object. Many complex work can be completed without having to make or other multimedia tools like hypertext identification languages.
Creating a new object in JavaScript is very simple. First it must define an object, then create an instance for the object. This instance is a new object that has the basic feature of the object definition.
First, the definition of the object
The definition of the JavaScript object, its basic format is as follows:
Function Object (Property)
This.prop1 = prop1 this.prop2 = prop2 ... this.meth = functionName1; this.meth = functionName2; ...
In an object's definition, it can indicate its properties and methods for this object. An example of an object is constituted by attributes and methods. As is the definition of the university object:
Function University (Name, City, Creatdate URL)
THIS.NAME = Name this.city = city this.creatdate = new date (create) this.url = URL
The basic meanings are as follows:
Name- Specify a "unit" name. City-"Unit" City. Creatdate-records the update date of the UNIVERSITY object. URL-This object points to a URL.
Second, create an object instance
Once the object definition is complete, you can create an instance for the object:
NewObject = new Object ();
Where newobjet is a new object, Object has defined a good object. example:
U1 = New University ("Yunnan Province", "Kunming", "January 05, 199712: 00: 00", "http://www.yn.km")
U2 = new University ("Yunnan University of Electronic Science and Technology", "Kunming", "January 07, 1997 12:00:00", "htlp://www.ynkj.cn")
Third, the use of object methods
In addition to the use of properties in the object, it is sometimes necessary. In the definition of the object, we see this.meth = functionName statement, that is, the method of defining objects. The method of substantive object is a function of functionName, which has achieved its intent by it.
Example Add a method in the university object that shows it itself and returns the corresponding string.
Function University (Name, City, CreateDate, URL)
THIS.NAME = Name; this.city = city; this.created); this.url = url; this.Showuniversity = showunance;
One of this.Showuniversity is defined 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 the internal function in JavaScript, which shows its string.
Fourth, array in JavaScript
Creating an array in JavaScript using NEW is not provided with a significant array type like other languages, but you can define an array with Function and create an array with a subscript using the NEW object operator. Thereby, any data type can be implemented. a, define the array of objects Function ArrayName (size) {this.length = size; for (var x =; x <= size; x ) THIS [x] = 0; Reture this;} where arrayname is a name for defining arrays Sub, size is the number of values (1-size), ie array elements. The array of current objects is defined by the For loop, and finally returns this array. As can be seen, the arrays in JavaScript are from 1 to size, which is different from other 0 to SIZE an array representation. Of course, you can adjust the subscript of the array from 1 to SIZE to 0 to Size-1 as needed. It can be implemented by the following: function arrayname (size) for (VAR x = 0; x <= size; x ) THIS [x] = 0; this.lengHt = size; return this; It can be seen from above is just adjustment The location of this.lenght, which is used to store the size of the array. Thus the subscript of the adjusted array will be consistent with other languages. However, the reader is noted that since the array subscript is from 1 to Size, the object functions in JavaScript are more powerful. b. Creating an array instance After a array definition is complete, you can't use it immediately, you must create an array instance for the array: myarray = new arrayname (n); and assigns the initial value: MyArray [1] = "string 1"; MyArray [2] = "String 2"; MyArray [3] = "String 3"; ... myarray [n] = "String N"; once the number is set to the initial value, there is an array True data is available, you can reference directly during programming. Create a multi-dimensional number of Function CreatMarray (Row, COL) {var index = 0; this.Length = (Row * 10) col for (var x = 1; x <= row; x ) for (var y = 1; y < = col; y ) index = (x * 10) y; this [index] = "";} mymarray = new createmarray (); after passing Mymarray [11], Mymarray [12], Mymarray [13], MyMarRay [21], mymarray [22], mymarray [23], ... Internal array
In Java In order to facilitate the operation of the internal object, you can use the Forms, Frames, Element, Links, and anchors anchor (anchors) arrays to implement object access.
Anchors []: Use the "a name =" anchorname "identifier to create an anchor link. Links []: Use the to define a super-text link item. Forms []: This array is established when using multi-forms in the program. Elements []: Use the array when you use from an element in a window. Frames []: When establishing a framework, use the array anchors [] Access to the form (it is specified by "form name =" form1 "), link [] is used to link to the anchor (It is specified by "a href = url"). Forms [] reflects the properties of the form, and Anchors [] reflects the link properties in the web page. Documents for anchors:
HTML CODE
HTML CODE
.
This document segment has established a link on both sides and access these anchors through Anchors []. Document.Anchors [0] reflects the first anchor, and Document.Anchors [1] reflects the information about the second anchor.
V.
Example 1: An example of a dynamic text scroll.
TEST5_1.HTM
With (Top.Window.Location)
{Baseurl = href.substring (0, href.lastindexof ("/") 1)}
TOTAL_TOC_ITEMS = 0;
Current_overid = "";
Last_overId = ""
Browser = navigator.Appname;
Version = parseint (navigator.appversion);
Client = NULL;
Loaded = 0;
IF (browser == "Netscape" && version> = 3)
Client = "ns3";
Function TOC_ITEM (IMG_NAME, ICON_COL, Width, Height) {
IF (client == "ns3") {
IMG_PREFIX = BaseURL IMG_NAME;
THIS.ICON_COL = icon_col;
THIS.TOC_IMG_OFF = New Image (Width, Height);
THIS.TOC_IMG_OFF.SRC = IMG_PREFIX "_OFF.GIF";
THIS.TOC_IMG_ON = New Image (Width, Height);
THIS.TOC_IMG_ON.SRC = IMG_PREFIX "_on.gif";
}
}
Function new_toc_item (IMG_NAME, ICON_ROW, Width, HEight) {
TOC_ITEM [IMG_NAME] = New TOC_ITEM (IMG_NAME, ICON_ROW, Width, Height);
Function TOC_MOUSEOVER (ItemID) {
IF (client == "ns3") {
Current_overid = ItemID;
IF (current_overid! = last_overid) {
Document [current_overid] .src = TOC_ITEM [current_overid] .toc_img_on.src;
IF (Last_Overid! = ") {
Document.images [last_overid] .src = TOC_ITEM [Last_Overid] .toc_img_off.src;
}
Last_overid = current_overid;
}
}
}
Function TOC_MOUSEOUT () {
IF (client == "ns3") {
IF (current_overid! = ") {
Document.Images [current_overid] .src =
TOC_ITEM [Current_Overid] .toc_img_off.src;
}
Current_overid = "";
Last_overId = ""
}
}
New_toc_item ("1", 2, 1220, 20);
Function bannerObject (p) {
THIS.MSG = Message
THIS.out = "" "
THIS.POS = position
THIS.DELAY = Delay
THIS.I = 0
this.Reset = ClearMessage}
Function ClearMessage () {
THIS.POS = position}
Var position = 50;
Var delay = 150;
Var message = "This is an example of dynamic JavaScript text display;
Var scroll = new bannerObject ();
Function scroller () {
Scroll.out = "";
IF (scroll.pos> 0)
For (scroll.i = 0; scroll.i {scroll.out = ""; IF (scroll.pos> = 0) Scroll.out = scroll.msg Else Scroll.out = scroll.msg.substring (-scroll.pos, scroll.msg.length) Document.noticeForm.notice.Value = scroll.out Scroll.out = ""; Scroll.pos -; Scroll.pos -; IF (scroll.pos <- (scroll.msg.length) {scroll.reset ();} settimeout ('scroller ()', scroll.delay); script> hEAD> Text = "# c0c0c0">