Asked by rwniceing
at 2024-08-22 00:54:34
Point:500 Replies:23 POST_ID:829189USER_ID:12079
Topic:
JavaScript;Java Programming Language;PHP Scripting Language
After studying prototype of javascript from article tutorial on this site
http://code.tutsplus.com/tutorials/prototypes-in-javascript--net-24949
In that article, it mentions purpose of prototype will use less memory usage for javascript
if creating many objects like 1000 game objects .In other words, all Game objects can use the same prototype in the function to save memory.
Now I try to create a simple example for tutorial to understand prototype easier.
I try to create a function for restaurant order price calculation according number of food and drink to order. So we need formula in a function for example
function CookMenu(food,drink){
this.price=3100*food+200*drink
return this
}
and I need object for every customer such as
var guest_Bob_price = new CookMenu(2,3); // guest_Bob_price.price =800
We could NOT change the code of the formula of the function during running besides re-write the code after code running. So if the owner of the restaurant suddenly want to
change CookMenu prices to pricechg=300*food+700*drink during applcation code is running , the prototype might be useful to change the internal structure of CookMenu function, Right ?
My question is that
1- How to update Cookmenu function for prototype without creating
many guest customer objects such as 1000 times of similar "var guest_Bob_price = new CookMenu(2,3);" if I have 1000 different guest or customers ?
2- Could I delete function property such as price when pricechg prototype property
is created ? Now guest_Bob_price.price =800 and after change, guest_Bob_price.pricechg =2700. In other words, I want to guest_Bob_price.price =2700 to replace pricechg.
Please read my following CookMenu code for example only and read console.log
and Please advise
Or you can suggest other good tutorial example besides mine that show a good or best
easy understanding the main purpose of prototype and application examples.
Rwniceing
http://code.tutsplus.com/tutorials/prototypes-in-javascript--net-24949
In that article, it mentions purpose of prototype will use less memory usage for javascript
if creating many objects like 1000 game objects .In other words, all Game objects can use the same prototype in the function to save memory.
Now I try to create a simple example for tutorial to understand prototype easier.
I try to create a function for restaurant order price calculation according number of food and drink to order. So we need formula in a function for example
function CookMenu(food,drink){
this.price=3100*food+200*drink
return this
}
and I need object for every customer such as
var guest_Bob_price = new CookMenu(2,3); // guest_Bob_price.price =800
We could NOT change the code of the formula of the function during running besides re-write the code after code running. So if the owner of the restaurant suddenly want to
change CookMenu prices to pricechg=300*food+700*drink during applcation code is running , the prototype might be useful to change the internal structure of CookMenu function, Right ?
My question is that
1- How to update Cookmenu function for prototype without creating
many guest customer objects such as 1000 times of similar "var guest_Bob_price = new CookMenu(2,3);" if I have 1000 different guest or customers ?
2- Could I delete function property such as price when pricechg prototype property
is created ? Now guest_Bob_price.price =800 and after change, guest_Bob_price.pricechg =2700. In other words, I want to guest_Bob_price.price =2700 to replace pricechg.
Please read my following CookMenu code for example only and read console.log
and Please advise
Or you can suggest other good tutorial example besides mine that show a good or best
easy understanding the main purpose of prototype and application examples.
Rwniceing