Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

javascript question: functionName.VariableName?

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');
  1. 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?)
  2. Modify the function so that the variable "next_item_id", cannot be modified during run time.
  3. 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.

Answer*

Draft saved
Draft discarded
Cancel
1
  • 1
    +1, This is the only correct answer given. It appears everyone else wanted to "solve" the test question instead of answering the OP's actual question. Commented Sep 11, 2011 at 15:04

lang-js

AltStyle によって変換されたページ (->オリジナル) /