View Single Post
Old 06-30-2012, 04:29 PM   PM User | #4
Sylvain_
New Coder

 
Join Date: Apr 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Sylvain_ is an unknown quantity at this point
Quote:
Originally Posted by DaveyErwin View Post
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>
If you can make it like this, I'm wondering why both in school and a site like codecademy learn the use of prototype. Or is there a difference between prototype and your approach?
Sylvain_ is offline   Reply With Quote