8.3.1 The with KeywordThe with keyword is used as a kind of shorthand for referencing an object's properties or methods . The object specified as an argument to with becomes the default object for the duration of the block that follows . The properties and methods for the object can be used without naming the object. (If a method is used, don't forget to include the parentheses after the method name .) FORMAT with (object){ < properties used without the object name and dot> } Example: with(employee){ document.write(name, ssn, address); } Example 8.9<html> <head><title>The with Keyword</title> <script language = "javascript"> 1 function book (title, author, publisher){ 2 this.title = title; // Properties this.author = author; this.publisher = publisher; 3 this.show = display; // Define a method } 4 function display (anybook){ 5 with(this) { // The with keyword 6 var info = "The title is " + title ; info += "\nThe author is " + author ; info += "\nThe publisher is " + publisher ; 7 alert(info); } } </script> </head> <body bgcolor="lightblue"> <script language = "javascript"> 8 var childbook = new book ("A Child's Garden of Verses", "Robert Lewis Stevenson", "Little Brown"); 9 var adultbook = new book ("War and Peace", "Leo Tolstoy", "Penguin Books"); 10 childbook.show(childbook); // Call method for child's book 11 adultbook.show(adultbook); // Call method for adult's book </script> </body> </html> EXPLANATION
Figure 8.9. The childbook object and its properties.
Figure 8.10. The adultbook object and its properties.
8.3.2 The for/in LoopJavaScript provides the for/in loop, which can be used to iterate through a list of object properties or array elements. The for/in loop reads: for each property in an object (or for each element in an array) get the name of each property (element), in turn , and for each of the properties (elements), execute the statements in the block that follows. The for/in loop is a convenient mechanism for looping through the properties of an object. FORMAT for(var property_name in object){ statements; } Example 8.10<html> <head><title>User-defined objects</title> <script language = "javascript"> 1 function book(title, author, publisher) { 2 this.title = title; this.author = author; this.publisher = publisher; 3 this.show=showProps; // Define a method for the object } 4 function showProps(obj, name) { // Function to show the object's properties var result = ""; 5 for (var prop in obj) { 6 result += name + " . " + prop + " = " + obj[prop] + "<br>"; } 7 return result; } </script> </head> <body bgcolor="lightblue"> <script language="javascript"> 8 myBook = new book("JavaScript by Example", "Ellie", "Prentice Hall"); 9 document.write("<br><b>" + myBook.show(myBook, "myBook" )); </script> </body> </html> EXPLANATION
8.3.3 Extending Objects with PrototypesObject-oriented languages support a feature called inheritance, where one object can inherit the properties of another. JavaScript implements inheritance with prototypes. As of Netscape Navigator 3.0, it is possible to add properties to objects after they have been created by using the prototype object. JavaScript functions are automatically given an empty prototype object. If the function serves as the constructor for an object, then the prototype object can be used to implement inheritance. When the properties are assigned to a given object by a constructor function, the prototype object gets the same properties. Each time a new object of the same class is created, that object also inherits the prototype object and all the same properties. The good news is that even after an object has been created, it can be extended with new properties that will also become part of the prototype . Then any objects created after that will automatically inherit the new properties. What Is a Class?In object-oriented languages, the object's data describes the properties. The object, along with its properties and methods, is bundled up into a container called a class , and one class can inherit from another, and so on. Even though JavaScript doesn't have a class mechanism per se, it mimics the class concept with the constructor and its prototype object. Each JavaScript class has a prototype object and one set of properties. Any objects created in the class will inherit the prototype properties. Let's say we define a constructor function called Employee() with a set of properties. The prototype object has all the same properties. The Employee() constructor function represents a class. The constructor is called and instantiates an object called janitor , and then the constructor is called again and instantiates another object called manager , and so on. Each instance of the Employee() class automatically inherits all the properties defined for the Employee through its prototype . After an object has been created, new properties can be added with the prototype property. This is how JavaScript implements inheritance. Example 8.11<html> <head><title>User-defined objects and Inheritance</title> <script language = "javascript"> 1 function Book (title, author, publisher){ // The Book class this.title = title; this.author = author; this.publisher = publisher; this.show=showProps; } 2 function showProps (obj,name){ var result = ""; for (var i in obj) { result += name + "." + i + " = " + obj[i] + "<br>"; } return result; } </script> </head> <body bgcolor="lightblue"> <script language="javascript"> // Add a new function 3 function lastEdition(){ this.latest=prompt("Enter the latest edition for "+this.title,""); return (this.latest); } // Add a new property with prototype 4 Book.prototype.edition=lastEdition; 5 var myBook=new Book("JavaScript by Example", "Ellie", "Prentice Hall"); // Define a new method document.write("<br><b>" + myBook.show(myBook,"myBook")+"<br>"); 6 document.write("The latest edition is "+ myBook.edition() +"<br>"); </script> </body> </html> EXPLANATION
Extending a JavaScript ObjectSince all objects have the prototype object, it is possible to extend the properties of a JavaScript built-in object, just as we did for a user-defined object. (See Chapter 9, "JavaScript Core Objects.") Example 8.12<html><head><title>Prototypes</title> <script language = "javascript"> // Customize String Functions 1 function uc() { 2 var str=this.big(); 3 return( str.toUpperCase()); } 4 function lc() { 5 var str=this.small(); 6 return( str.toLowerCase()); } 7 String.prototype.bigUpper=uc; 8 String.prototype.smallLower=lc; 9 var string="This Is a Test STRING."; 10 string=string.bigUpper() ; document.write(string+"<br>"); 11 document.write( string.bigUpper() +"<br>"); 12 document.write( string.smallLower() +"<br>"); </script> </head> <body bgcolor="lightblue"></body> </html> EXPLANATION
|