JavaScript Tutorial - from entry to the master (5)
Related Tags:
In the creation of new objects in JavaScript
JavaScript can be used to create your own objects. Although internal JavaScript and the functions of the browser itself has been very strong, but still provides JavaScript to create a new object approach. Its not like hypertext markup language, or for the other multimedia tools, we will be able to complete many of the complex work.
In JavaScript to create a new object is very simple. First, it must be defined an object, and then creates an instance of the object. This example is a new object, it has object definition of the Basic characteristics.
First, the definition of target
JavaScript objects to the definition, and the basic format is as follows:
Function Object (Attribute Table)
This.prop1 = prop1
This.prop2 = prop2
...
This.meth = FunctionName1;
This.meth = FunctionName2;
...
In the definition of an object, the object can be specified for its properties and methods. Through the properties and methods constitute an object example. As the following is a target on the definition University:
Function university (name, city, creatDate URL)
This.name = name
This.city = city
This.creatDate = New Date (creatDate)
This.URL url =
Its basic meaning of the following:
Name-designation of a "unit" name.
City-"unit" of the city.
CreatDate-targeted university records updated.
- The object at the URL of a Web site.
Second, once the target object instance to create after the completion of the definition, we can create an object for example:
New object NewObject = ();
Newobjet which is a new object, Object object has been defined. Example:
New university U1 = ( "Yunnan", "Kunming", "January 05,199712:00:00", "http://www.YN.KM")
New university U2 = ( "Yunnan University of Electronic Science and Technology", "Kunming", "January 07,1997 12:00:00", "htlp: / / www.YNKJ.CN")
Third, the use of Object
In addition to the use of attributes in the object, and sometimes need to use the method. In the definition of targets, we see This.meth = FunctionName statement, which is the method for defining object. Real object method is a function FunctionName, through its own intentions.
In the case of an increase in the university object, the method is demonstrated its own, and to return to 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;
This.showuniversity which is the definition of a solution - showuniversity ().
The showuniversity () method is to achieve university object itself is displayed.
Function showuniversity ()
For (var prop in this)
Alert (prop = "this [prop]" ");
JavaScript alert which is the internal function, indicating their string.
New use of the array
JavaScript does not provide a Clear like other languages of the array type, but can be defined through an array of function, operation and use of objects at New create a next target array. So that we can achieve any type of data storage.
A definition of the object array
Function arrayName (size) (
This.length = Size;
For (var X =; X <= size; X)
This [X] = 0;
Reture this;
)
ArrayName definition of the array which is a name, size is the size of the array of (1 - size), which is the number of array elements. Adopted for the current cycle of a target array definition, the final return to this array.
As can be seen, in the JavaScript array size from 1 to this and other 0 to size of the array is different from that approach, of course, you may need to array subscript from 1 to adjust to the size 0 to size-1, the following can be achieved:
Function arrayName (size)
For (var X = 0; X <= size; X)
This [X] = 0;
This.lenght = size;
Return this;
From the above we can see that this method is only adjusted the this.lenght position, the Position is for the size of the storage array. After adjustments to the array subscript will be consistent with other languages. But it is precisely because the attention of readers array subscript order from 1 to size, making JavaScript objects more powerful.
B, create an array of examples of an array after the completion of the definition, was not immediately be used to create an array of the array examples:
New arrayName Myarray = (n);
Initial and empowerment:
Myarray [1] = "a string";
Myarray [2] = "string 2";
Myarray [3] = "string 3";
...
Myarray [n] = "string n";
Once assigned to the initial array, the array has the real meaning of the data, since the procedure can be applied directly in the design process.
Create multi-dimensional arrays
Function creatMArray (row, col) (
Var indx = 0;
This.length = (10 * row) col
For (var x = 1; x <= row; x)
For (var y = 1; y <= col; y)
Indx = (x * 10) y;
This [indx ]="";
)
MyMArray = new creatMArray ();
After myMArray through [11], [12] myMArray, myMArray [13], [21] myMArray, myMArray [22], [23] myMArray,
… To invoke.
Internal array in Java in order to facilitate the operation of the internal object, you can use the form (Forms), the framework (Frames), elements (element), links (links) and the anchor (Anchors) array to achieve targets visit.
[] Anchors: "A name =" anchorName "" logo to establish a link anchor.
Links []: Use <A href = "URL"> to a definition of the text links.
Forms []: In the process of using multiple forms, the establishment of the array.
Elements []: the use of a window from the elements, to establish the array.
Frames []: the creation of a framework, the use of the array
[] Anchors for the form of visits (it is through "form name =" form1 "" designated), the link [are] used to be linked to the anchor of the visit (It is through "a href = URL" Specified). Forms [] reflect the attributes window, and anchors reflect [] the links in Web pages attributes.
The anchor array of files:
<HTML>
<HEAD>
<BODY>
<A NAME="MyAnchorsName1"> definition of an anchor of
html Code
<A NAME="MyAnchorsName2"> definition of a second anchor
HTML Code
<A HREF="#MyAnchorsName1"> establishment of the link anchor
<A HREF = "# MyAnchorsName2? Gt;… establishment of the link anchor.
The documents of the two sides established a full-anchor links through Anchors [] visit the anchor. Document.Anchors [0] reflect the first anchor, and document.Anchors [1] The second anchor reflect the relevant information.
Example 1: a dynamic example of the rolling text.
Test5_1.htm
<html>
<head>
<title> </ Title>
<script LANGUAGE="JavaScript">
With (top.window.location)
(Href.substring baseURL = (0, href.lastIndexOf ("/") 1))
Total_toc_items = 0;
Current_overID = "";
Last_overID = "";
= Navigator.appName browser;
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,120,20);
<! - Begin
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 a dynamic text Display JavaScript examples";
Var scroll = new bannerObject ();
Function scroller () (
Scroll.out = "";
If (scroll.pos> 0)
For (scroll.i = 0; scroll.i <scroll.pos; scroll.i) (scroll.out = "";)
If (scroll.pos> = 0)
Scroll.out = scroll.msg
Else
Scroll.msg.substring scroll.out = (-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>
<Body onload = "scroller ()" bgcolor = "# 000000" link = "# C0C0C0" vlink = "# C0C0C0"
Alink = "# 008080"
Text = "# C0C0C0">
<table Border="0" cellspacing="0" cellpadding="0">
<tr>
<td Width="100%"> <form NAME="noticeForm">
<p> <input TYPE="text" name="notice" size="40" style="background-color: rgb(192192192)"> </ p>
</ Form>
</ Td>
</ Tr>
</ Table>
</ Center> </ div>
</ Body>
</ Html>
Example 2: Examples of Color changes.
Test5_2.htm
<html>
<head>
<script>
<! --
Function makearray (n) (
This.length = n;
For (var i = 1; i <= n; i)
This [i] = 0;
Return this;)
Hexa = new makearray (16);
For (var i = 0; i <10; i)
Hexa [i] = i;
Hexa [10] = "a";
Hexa [11] = "b";
Hexa [12] = "c";
Hexa [13] = "d";
Hexa [14] = "e";
Hexa [15] = "f";
Function hex (i) (
If (i <0)
Return "00";
Else if (i> 255)
Return "ff";
Else return "," hexa [Math.floor (i/16)] hexa [i];)
Function setbgColor (r, g, b) (
Var hr = hex (r);
Var hg = hex (g);
Var hb = hex (b);
Document.bgColor = "#" hr hg hb;)
Function fade (sr, sg, sb, er, eg, eb, step) (
For (var i = 0; i <= step; i) (
SetbgColor (Math.floor (sr * ((step-i) / step) er * (i / step)),
Math.floor (sg * ((step-i) / step) eg * (i / step)), Math.floor (sb *
((Step-i) / step) eb * (i / step)));))
Function fadein () (
Fade (255,0,0,0,0,255,100);
Fade (0,0,255,0,255,0,100);
Fade (0,255,0, 0,0,0, 100);)
Fadein ();
/ / ->
</ Script>
<body>
</ Body>
</ Html>
The stresses on the user to create objects, users may need to create their own objects. JavaScript and introduced an array of construction methods.
- Ajax master series
- Destination establishment - from entry to the master - basic knowledge
- Fast proficient in the use of master framework
- Web master - page scroll Break
- GMail view from the W3C Web standards and Ajax
- XHTML + CSS + JavaScript Learning Resources (major sites, W3C School)
- W3c technical architecture introduced
- Destination establishment - from entry to the master - chapter details
- W3c validator: CSS/HTML4.01/XHTML1.0 XiaoZhu.com have passed smoothly:)
- WEB master HTML chapter of the courses
- Destination establishment - from entry to the master - for articles
- CSS learning from entry to the master
- Blog.csdn.net support of the W3C CSS problems
- Google entry to the master
- W3C school HTML help guide CHM
- Destination establishment - from entry to the master (thematic)
- Part V reference master Ajax
- Web standards: XHTML + CSS (W3C)
- W3C XHTML code norms
- Fast proficient in the use of master FRAME




