View Single Post
Old 06-30-2012, 04:02 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
if you are trying to make it
like C# or Java then don't
use prototype
make it more like this ...

Code:
<script>
function CashRegister() {
    this.total = 0;
    this.items = new Array();
    this.addItem = function addItem(item){
	this.items.push(item);	
    }
}
function Item(name, unityPrice, category) {
    this.name = name;
    this.unityPrice = unityPrice;
    this.category = category;
}
var cola=new Item("cola", 2, "drinks")
var tea=new Item("tea", 4, "drinks")
var myCash = new CashRegister();
myCash.addItem(cola);
myCash.addItem(tea);
// because myCash.items is an array do this ..,
for (var i = 0; i < myCash.items.length; i++) {
    document.writeln(myCash.items[i].name);
}
</script>

Last edited by DaveyErwin; 06-30-2012 at 04:24 PM..
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
Sylvain_ (06-30-2012)