HTML and CSS Reference
this.price *= 1.18;
var famousBook=new Book("War and Peace","Leo Tolstoy", 30.00);
document.write("<br /><b>" + famousBook.show()+"<br />");
document.write("<br /><b>" + myBook.show()+"<br />");
document.write("With tax \""+ famousBook.title +"\" costs $"+
famousBook.addTax() +".<br />");
document.write("With tax \""+ myBook.title + "\" costs $" +
myBook.addTax() +".<br />");
The constructor function called Book defines the properties and methods for a
tion has a prototype object containing those properties that are inherited by all
A function called showProps is defined on line 3. Its name is assigned to the prop-
erty called show , making it a method for the Book class.
The function showProps uses the special for loop to iterate through all the proper-
ties of an object.
The Book's prototype property is used to add a function called addTax() . It calcu-
lates and returns the price of the book with an 18 percent sales tax added. This
function will be added as a method to the Book class and available to all objects
will point to that copy.
A new Book object, called myBook , is created. It has inherited all of the original
properties of the Book class, including the new method defined by the prototype
property, called addTax .
The show() method is called for the object, famousBook . The entire object, prop-
erties and methods are displayed. You can see that addTax has been included.
The new method, addTax() is called for the object famousBook .
Again, addTax() is called for the object myBook . The result is shown in Figure