Java Reference
In-Depth Information
<h4>Professional Ajax</h4>
<p>
<img src=”pro_ajax.jpg” alt=”Professional Ajax, 2nd Edition” border=”0”
onclick=”showDetails('pro_ajax_details.htm')” />
</p>
<h4>Professional JavaScript for Web Developers</h4>
<p>
<img src=”pro_js.jpg” alt=”Professional JavaScript, 2nd Edition” border=”0”
onclick=”showDetails('pro_js_details.htm')” />
</p>
</body>
</html>
The details of the topics are stored in the stockItems array, which will contain StockItem objects, a
reference type you defi ne with the StockItem() constructor function.
function StockItem(title, price)
{
this.title = title;
this.price = price;
this.quantity = 0;
}
Objects created by this function have title , price , and quantity properties. The fi rst two are
assigned values from the two parameters of the same name, and the third initializes as 0 .
So you populate the stockItems array fi rst, with each element containing a StockItem object as the
following code shows:
var stockItems = new Array();
stockItems[0] = new StockItem(“Professional Ajax, 2nd Edition”, “$39.99”);
stockItems[1] = new StockItem(“Professonal JavaScript, 2nd Edition”, “$46.99”);
The fi rst function defi ned in the code is removeItem() .
function removeItem(stockId)
{
stockItems[stockId].quantity = 0;
alert(“Item Removed”);
showBasket();
return false;
}
This removes a book from the shopping basket. It accepts one parameter called stockId , the array
element index of that book, which you use to set the quantity property to 0 .
Next, you have the function that adds a book to the shopping basket, addBookToBasket() .
function addBookToBasket(stockId)
{
stockItems[stockId].quantity = 1;
alert(“Item added successfully”);
Search WWH ::




Custom Search