|
The calling object is considered global inside a method. You can modify the object using the word this. Creating Objects—An ExampleLet’s say you want to create a database-like object structure for a store that sells televisions. Each type of television should be an instance of the same object. The number of televisions of a certain model available in stock should be a property of the object. It should also include the features of that television set. In addition, the object should include two methods: one to be invoked when a customer buys a television set, and another to be invoked when the store owner orders a certain quantity of television sets of a given model. Example 12-2 demonstrates the most important points.
<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- function television(brand, size, price, num) { this.brand = brand // property this.size = size // property this.price = price// property this.num = num // property this.sell = sell // method this.buy = buy // method this.display = display // method } function sell(quantity) { if (quantity > this.num) alert("Not enough " + this.brand + " " + this.size + "\" sets in stock.") else this.num -= quantity if (quantity < 5) alert("Order more " + this.brand + " " + this.size + "\" sets urgently.") } function buy(quantity) { this.num += quantity } function display() { var result = "" result += "<TABLE BORDER=2><TR>" result += "<TD WIDTH = 60>" + this.brand + "</TD>" result += "<TD WIDTH = 30>" + this.size + "\"</TD>" result += "<TD WIDTH = 45>$" + this.price + "</TD>" result += "<TD WIDTH = 45>" + this.num + " left</TD>" result += "</TR></TABLE>" document.write(result) } var tel1 = new television("Sony", 27, 1200, 30) var tel2 = new television("JVC", 20, 650, 20) var tel3 = new television("Grundig", 14, 420, 45) tel1.sell(27) // 27 "Sony" television sets sold tel1.buy(16) // 16 "Sony" television sets ordered (bought) tel1.display() tel2.sell(21) // 21 "JVC" television sets sold -- error! tel2.sell(1) // 1 "JVC" television set sold tel2.display() tel3.display() // --> </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
Example 12-2. A script that stores data using instances of a general object. Example 12-2 features three methods:
Notice that the first two methods actually change values of the object. This is another example that shows that methods refer to their calling object as global, so all properties of the object modified inside the function (method) are also affected outside the method. Prototype and Object ExtensionsA prototype is a property of any object created with the keyword new. A prototype resembles the common e-mail address portion of users sharing the same server. Likewise, a prototype belongs to all instances of the object that the prototype belongs to. Prototypes refer not only to properties, but also to an object’s methods. Take a look at the following script segment:
var str1 = new String("a"), str2 = new String("b") function repeat(n, delimiter) { var text = "" var str = this.toString() // make sure the object is string while (n >= 0) { text += str text += delimiter n-- } return text } String.prototype.repeat = repeat // String == name of object // add a repeat() method to String alert(str1.repeat(5, " ; ")) alert(str1) alert(str2.repeat(7, " ")) alert(str2.repeat(3))
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |