Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're going about it all wrong. I'm assuming that you want to set internal variables.</p> <p>If you want to create a 'constructor', do something like this:</p> <pre><code>function Item(itemName) { var next_item_id = 1; this.item_name = itemName; this.item_id = next_item_id++; } var Item1 = new Item('Desktop'); var Item2 = new Item('Laptop'); var Item3 = new Item('Monitor'); </code></pre> <p>Both item_name and item_id will be publicly available. To make use next_item_id like you want, try this:</p> <pre><code>Item.next_item_id = 1; </code></pre> <p>Then in your constructor, do Item.next_item_id++;</p> <p>Your final code should look something like this:</p> <pre><code>function Item(itemName) { this.item_name = itemName; this.item_id = Item.next_item_id++; } Item.next_item_id = 1; var Item1 = new Item('Desktop'); var Item2 = new Item('Laptop'); var Item3 = new Item('Monitor'); </code></pre> <p>What this is doing is attaching a property to the object Item. You might be thinking, "Item is a function! How is this possible?!". Nearly <strong>everything</strong> in JavaScript is an object with mutable properties. The only things that aren't are the keywords null and undefined (AFAIK).</p> <p>Item is still a function, but it also has a property next_item_id.</p> <p>Also, using new will create a new instance. This is a similar concept as in Java and other programming languages. Just calling Item is like calling a function, and you'll get back whatever is returned from it (through an explicit return statement, otherwise undefined).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload