Previous Example-|-Next Example-|-Return to Chapter Listing

Example 13.2:
Creating an Object Hierarchy--Properties Can be Objects


This form displays all the properties for each object created. Object class 'order' has the object class 'product' as a property.


This page uses the following to create the objects: <SCRIPT LANGUAGE="JavaScript"> <!-- //========================================================================== function product(nam, p) { this.name = nam this.price = p } //========================================================================== function order(custname, prod, numbof) { this.customer = custname this.ProductType = prod this.quantity = numbof this.OrderDate = new Date() } var product1 = new product("TimeSaver", "$23") var product2 = new product("MoneySaver", "$11") var product3 = new product("Time n'MoneySaver", "$99") var order1 = new order("Mr Brainless", product1, 5) var order2 = new order("Mr Bonkers", product3, 22) var order3 = new order("Mr Bozo", product2, 1) //========================================================================== //--> </SCRIPT> Then we write the object properties like this: <SCRIPT LANGUAGE="JavaScript"> <!-- //====================================================================== document.write("NAME: " + order1.customer + "<BR>QUANTITY: " + order1.quantity + " ") document.write("<BR>DATE: " + order1.OrderDate) document.write("<BR>PRODUCT TYPE: " + order1.ProductType.name + "<BR>PRICE: " + order1.ProductType.price + "<P>") document.write("NAME: " + order2.customer + "<BR>QUANTITY: " + order2.quantity + " ") document.write("<BR>DATE: " + order2.OrderDate) document.write("<BR>PRODUCT TYPE: " + order2.ProductType.name + "<BR>PRICE: " + order2.ProductType.price + "<P>") document.write("NAME: " + order3.customer + "<BR>QUANTITY: " + order3.quantity + " ") document.write("<BR>DATE: " + order3.OrderDate) document.write("<BR>PRODUCT TYPE: " + order3.ProductType.name + "<BR>PRICE: " + order3.ProductType.price + "<P>") //--> </SCRIPT>
Previous Example-|-Next Example-|-Return to Chapter Listing