Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

The calling object is considered global inside a method. You can modify the object using the word this.

Creating Objects—An Example

Let’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:

  • sell()
    This method modifies the number of television sets of the model number specified in the object. The function’s only parameter is one that accepts the number of television sets sold. It checks if there are enough sets in stock, because you cannot sell more than you’ve got! If there are enough sets of that type in stock, the number is subtracted from the total quantity of that TV available before the transaction. Lastly, the method checks if there are less than five TV sets of that type left. If so, it alerts a message describing that situation.
  • buy()
    This method simply adds the number of television sets to the num property of the object, or instance. The only parameter accepted by the method is the one that accepts the number of television sets bought.
  • display()
    This method does not accept any arguments. It simply prints all the properties of the current object in a tabular format.

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 Extensions

A 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))

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us