152 . Chapter 10 You need to include
Friday, August 31st, 2007152 . 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.