I had the following question in a test today. But i had not see something like functionName.VariableName before. Not sure how that works.
Would be great if you can tell me the solution:
function Item(itemName)
{
var next_item_id = 1;
Item.item_name = itemName;
Item.item_id = next_item_id++;
}
var Item1 = Item('Desktop');
var Item2 = Item('Laptop');
var Item3 = Item('Monitor');
- Anything wrong with the code above? if yes fix it. (The problem i would see is next_item_id is always 1, need to make it global?)
- Modify the function so that the variable “next_item_id”, cannot be modified during run time.
- My own question, how does the variable like Item.item_name work? I want to google it, but not sure what I should search for.
Thanks.