Create a new object in JavaScript

xiaoxiao2021-03-06  52

(Li Xiaohua April 29, 2001 01:50) Use JavaScript to create its own object. 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 Table) this.prop1 = prop1this.prop2 = Prop2 ... this.meth = functionName1; this.meth = functionName2; ... in one In the definition of an object, 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 = namethis.city = citythis.creatdate = new date (create) this.url = URL The basic meaning is as follows: Name- designated A "unit" name. City-"Unit" City. Creatdate-records the update date of the UNIVERSITY object. URL-This object points to a URL. 2. Once the object instance is created, once the object definition is complete, you can create an instance for this object: newObject = new object (); where newobjet is a new object, Object has defined a good object. example:

U1 = New University, "Yunnan", "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") 3. The use of the object method is in addition to the use of attributes in the object, sometimes it needs to be used. 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.createDate = New Date (creatDate); This.URL = URL; This.showuniversity = showuniversity; wherein is This.showuniversity Define 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, showing its string four, and arrays in JavaScript use New to create arrays JavaScript There is no significant array type like other languages, but you can define an array with Function and create an array of subscripts using the New object operator. To implement any data type storage .a, 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 of the array, size is related to the size of the array Value (1-size), ie the number of array elements. Define the array of current objects via the for loop, and finally returns this array. From it can be seen, the arrays in JavaScript are from 1 to Size, this with other 0 To the array representation method of size, of course, you can adjust the subscript of the array from 1 to Size to 0 to SIZE-1 as needed, can be implemented by the following: function arrayname (size) for (VAR x = 0; x < = size; x ) this [x] = 0; this.lenght = size; return th; It can be seen from above that this method is just the position 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. But the reader is not to note that the number of subscripts from the array is 1 to Size so that 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) colfor (var x = 1; x <= row; x ) for (var y = 1; y <= COL; Y ) index = (x * 10) y; this [index] = "";} mymarray = new createmarray (); can then pass mymarray [11], mymarray [12], mymarray [13], mymarray [ 21], MyMarray [22], Mymarray [23], ... Internal arrays In Java In order to facilitate operation of internal objects, you can use the Forms, Frames, Element, Links, and Anchors anchor (anchors) 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: defined the first anchored HTML code defined the second anchored HTML Code Connect the anchor link 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) {docuument [current_overid] .src = TOC_ITEM [current_overid] .toc_img_on.src; if (last_overid! = "") {DOC ument.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,120,20);! <- Beginfunction bannerObject (p ) {this.msg = messagethis.out = ""

this.pos = positionthis.delay = delayth.i = 0this.reset = ClearMessage} Function ClearMessage () {this.pos = position} var posation = 50; var delay = 150; var message = "This is a dynamic javascript text display Example "; var scroll = new bannerObject (); function scroller () {scroll.out =" "; if (scroll.pos> 0) for (scroll.i = 0; scroll.i = 0) scroll.out = scroll.msgelsescroll.out = scroll.msg.substring (-Scroll.pos, scroll.msg.length) Document.noticeform.notice.Value = scroll.terscroll.out = ""; scroll.pos -; scroll.pos -; if (scroll.pos <- (scroll.msg.length) {scroll.reset () } setTimeout ('scroller ()', scroll.delay);}

< / body> Example 2: An example of color change. TEST5_2.HTM

转载请注明原文地址:https://www.9cbs.com/read-113213.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.034, SQL: 9