Content deleted Content added
DanielPharos (talk | contribs) m →Delphi: Corrected a link |
DarthKitty (talk | contribs) m →JavaScript: use <code> tags where appropriate; make code snippets more idiomatic |
||
Line 589:
====Map and WeakMap====
Modern JavaScript handles associative arrays, using the <code>Map</code> and <code>WeakMap</code> classes. A map does not contain any keys by default
=====Creation=====
Line 595:
<syntaxhighlight lang=JavaScript>
["Sally Smart", "555-9999"],
["John Doe", "555-1212"],
["J. Random Hacker", "553-1337"],
]);
</syntaxhighlight>
Line 605:
<syntaxhighlight lang=JavaScript>
phoneBook.set("Sally Smart", "555-9999");
phoneBook.set("John Doe", "555-1212");
Line 612:
=====Access by key=====
Accessing an element of the map can be done with the
<syntaxhighlight lang=JavaScript>
</syntaxhighlight>
In this example, the
=====Enumeration=====
Line 625:
<syntaxhighlight lang=JavaScript>
// loop through the collection and display each entry.
for (const [name, number] of phoneBook) {
console.log(
}
</syntaxhighlight>
Line 641:
However, there are important differences that make a map preferable in certain cases. In JavaScript an object is a mapping from property names to values—that is, an associative array with one caveat: the keys of an object must be either a string or a symbol (native objects and primitives implicitly converted to a string keys are allowed). Objects also include one feature unrelated to associative arrays: an object has a prototype, so it contains default keys that could conflict with user-defined keys. So, doing a lookup for a property will point the lookup to the prototype's definition if the object does not define the property.
An object literal is written as <code>{ property1
<syntaxhighlight lang=JavaScript>
};
</syntaxhighlight>
To prevent the lookup from using the prototype's properties, you can use the <code>Object.setPrototypeOf</code> function:
<syntaxhighlight lang=JavaScript>
Line 657:
</syntaxhighlight>
As of ECMAScript 5 (ES5), the prototype can also be bypassed by using <code>Object.create(null)</code>:
<syntaxhighlight lang=JavaScript>
Object.assign(myObject, {
});
</syntaxhighlight>
Line 672:
<syntaxhighlight lang=JavaScript>
</syntaxhighlight>
Line 685:
<syntaxhighlight lang=JavaScript>
for (
}
</syntaxhighlight>
Line 694:
<syntaxhighlight lang=JavaScript>
for (const [property, value] of Object.entries(myObject)) {
}
</syntaxhighlight>
Line 708:
<syntaxhighlight lang=JavaScript>
myObject[1]
myObject[[
myObject[{ toString
</syntaxhighlight>
In modern JavaScript it's considered bad form to use the Array type as an associative array. Consensus is that the Object type and <code>Map</code>/<code>WeakMap</code> classes are best for this purpose. The reasoning behind this is that if Array is extended via prototype and Object is kept pristine,
See [http://blog.metawrap.com/2006/05/30/june-6th-is-javascript-array-and-object-prototype-awareness-day/ JavaScript Array And Object Prototype Awareness Day] for more information on the issue.
|