Archive for August, 2007

152 . Chapter 10 You need to include

Friday, August 31st, 2007

152 . Chapter 10 You need to include a reference to this function in the constructor function. This is done exactly like you define properties: function displayStudent() { var result = “” result += this.name+”–” result += “a ” + this.age+”-year old” result += this.grade + “% average student.
” result += this.name + “’s parents –” result += this.parents.father + “, ” + this.parents.mother result += “.
” document.write(result) } function student(name, age, grade, father, mother) { this.name = name this.age = age this.grade = grade this.parents = new parents(father, mother) this.display = displayStudent } function parents(father, mother) { this.father = father this.mother = mother } The following statements create an instance and invoke the display() method: var student1 = new student(”Sharon”, 16, 85, “Mark”, “Stacy”) student1.display() Notice the extensive use of the keyword this inside the function to refer to the object. The main characteristic of a method is that it usually processes the data of its object. You can even create a constructor method in the following fashion: function construct(name, val) { this[name] = val } function student(name, age, grade, father, mother) { this.construct = construct this.name = name this.age = age this.grade = grade this.parents = new parents(father, mother) } function parents(father, mother) { this.father = father this.mother = mother }
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Building and Extending Objects . 151 document.write(getProps(student1,”student1″)) //–>

Friday, August 31st, 2007

Building and Extending Objects . 151 document.write(getProps(student1,”student1″)) //–> Example 10-1. A script that uses getProps() to analyze the structure of an object. The output for the script in Example 10-1 looks like Figure 10-1: Figure 10-1. Output for the script in Example 10-1. Defining Methods Objects group data and functions that process that data. Data appears in the form of properties. A method is simply a function associated with an object. Consider the following function: function displayStudent() { var result = “” result += this.name+”–” result += “a ” + this.age+”-year old” result += this.grade + “% average student.
” result += this.name + “’s parents –” result += this.parents.father + “, ” + this.parents.mother result += “.
” document.write(result) } Chapter
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

150 . Chapter 10 // loop through properties (Web site designers)

Friday, August 31st, 2007

150 . Chapter 10 // loop through properties for (var i in obj) { // if current property is an object call function for it if (typeof obj[i] == “object”) result += getProps(obj[i], objName + “.” + i) else result += objName + “.” + i + ” = ” + obj[i] + “
” } // return final result return result } The function s algorithm is fairly simple. It loops through the properties of the main object. If the current property, represented by i, is an object, the function is called once again with the property obj[i] as the object, and the property s name attached to object s name with a separating dot (objName + “.” + i). Each call to the function returns the string listing the properties at the current level. The value returned by a recursive call is assigned to the variable result which is local in the calling function. Here is an entire HTML document and its output to help you understand this concept: Printing properties of nestedobjects Example 9-4(ex9-4.htm). You can call a JavaScript script via an event handler. Try loading this page and clicking the button. If you are using Navigator 3.0x or 4.0, an alert box displays the message This is the second function, despite the fact that the event handler associates the click event with the first function, alert1(). Notice the script placed directly after the form. It associates a different function with the click event of button1 in form1. Note: Event handlers are function references as opposed to function calls. Therefore, you must assign alert2, not alert2(), which is primarily a function call, evaluating to the type and value the function returns. Also, since the event handler HTML attributes are literal function bodies, you cannot use in the HTML source. You must call the function instead. You are probably wondering why we used onclick rather than onClick. The reason is that JavaScript is case sensitive and understands onclick (all lowercase), whereas HTML is case insensitive. In HTML, you may use all lowercase, all uppercase, or any other convention you choose. Calling event handlers explicitly enables flexible event handlers, because you can modify them anywhere in the script. You can create a customized page, allowing the user to determine what a certain button should do when clicked. An event handler does not have to exist when you use this technique. The preceding script would workexactly the same if you omitted the event handler specification in the HTML source and used the following line instead: In this case you are actually defining a new event handler to correspond with an event. Also note that the above method is the only way to assign a function reference to an event handler. You cannot use any of the following structures: