This method provides a way to extract an item from the Dictionary using its key name as an accessor.
This method can also be used as an alternative to the Dictionary.add() method although its syntax for doing so is unusual.
These two lines of code are syntactically different but functionally the same when creating a new name-value pair:
myDictionary.add("KEY1", "an item text");
myDictionary.item("KEY1") = "an item text";
This suggests that you can use the add() method to replace an item but in fact that will cause an error. The item() ,method, used as if it were an LValue property is the only way to replace an item content.
Although this is a method, because it has parentheses and therefore can be called, it also behaves as a property. This somewhat bends the syntax rules for JavaScript and looks like a badly formed expression when you see the variant that does an assignment.
It is important to use this item within a conditional code block that tests for the existence of the key first. This is because an attempt to retrieve a named item from a Dictionary will create a named but empty item if it does not already exist.
Note the capitalised name of the method.
Prev | Home | Next |
Dictionary.Exists() | Up | Dictionary.Items() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |