Content deleted Content added
Kaltenmeyer (talk | contribs) m →[[OptimJ]]: clean up |
|||
Line 545:
=== [[JavaScript]] ===
JavaScript (and its standardized version: [[ECMAScript]]) is a [[Prototype-based programming|prototype-based]] [[Object-oriented programming|object-oriented]] language. In JavaScript an object is a mapping from property names to values—that is, an associative array with one caveat: since property names are strings, only string and (coerced) integer keys are allowed. Other than that difference, objects also include one feature unrelated to associative arrays: a prototype link to the object they inherit from. Doing a lookup for a property will forward the lookup to the prototype if the object does not define the property itself.▼
JavaScript (and its standardized version: [[ECMAScript]]) is a [[Prototype-based programming|prototype-based]] [[Object-oriented programming|object-oriented]] language.
====Map and WeakMap====
Modern JavaScript handles associative arrays using Map and WeakMap classes. A map does not contain any keys by default. It only contains what is explicitly put into it. The keys and values can be any value (including functions, objects, or any primitive).
=====''Creation''=====
A map can be initialized with all items during construction:
<source lang=JavaScript>▼
var phoneBook = new Map([
["Sally Smart", "555-9999"],
["John Doe", "555-1212"],
["J. Random Hacker", "553-1337"]
]);
</source>▼
Alternatively, you can initialize an empty map and then add items:
<source lang=JavaScript>
var phoneBook = new Map();
phoneBook.set("Sally Smart", "555-9999");
phoneBook.set("John Doe", "555-1212");
phoneBook.set("J. Random Hacker", "553-1337");
</source>
=====''Access by key''=====
Accessing an element of the map can be done with the get method:
<source lang=JavaScript>
var sallyNumber = phoneBook.get("Sally Smart");
</source>
In this example, the sallyNumber value will now contain the string "555-9999".
=====''Enumeration''=====
The keys in a map are ordered. Thus, when iterating over it, a map object returns keys in order of insertion. The following demonstrates enumeration using a for-loop:
<source lang=JavaScript>
// loop through the collection and display each entry.
for(const [name, number] of phoneBook) {
console.log(`Phone number for ${name} is ${number}`);
}▼
</source>
A key can be removed as follows:
<source lang=JavaScript>
phoneBook.delete("Sally Smart");
</source>
====Object====
An object is similar to a map—both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. For this reason (and because there were no built-in alternatives), objects have been used as maps historically.
▲
An object literal is written as <code>{ property1 : value1, property2 : value2, ... }</code>. For example:
Line 555 ⟶ 614:
"J. Random Hacker" : "553-1337"
};
</source>
To prevent the lookup to the prototype, you can use Object.setPrototypeOf function:
<source lang=JavaScript>
Object.setPrototypeOf(myObject, null);
</source>
As of ES5, this can also be bypassed by using Object.create(null):
<source lang=JavaScript>
var myObject = Object.create(null);
Object.assign(myObject, {
"Sally Smart" : "555-9999",
"John Doe" : "555-1212",
"J. Random Hacker" : "553-1337"
});
</source>
Line 570 ⟶ 646:
</source>
You can also loop through all enumerable properties and associated values as follows (a for-in loop):
<source lang=JavaScript>
for(var property in myObject) {
var value = myObject[property];
}
</source>
Or (a for-of loop):
<source lang=JavaScript>
for(const [property, value] of Object.entries(myObject)) {
console.log(`${property} = ${value}`);
}
</source>
Line 585 ⟶ 669:
</source>
As mentioned before, properties are strings
<source lang=JavaScript>
Line 593 ⟶ 677:
</source>
In modern JavaScript it's considered bad form to use the Array type as an associative array. Consensus is that the Object type
▲<source lang=JavaScript>
▲}
▲</source>
▲In modern JavaScript it's considered bad form to use the Array type as an associative array. Consensus is that the Object type is best for this purpose. The reasoning behind this is that if Array is extended via prototype and Object is kept pristine, 'for(in)' loops will work as expected on associative 'arrays'. This issue has been drawn into focus by the popularity of JavaScript frameworks that make heavy and sometimes indiscriminate use of prototype to extend JavaScript's inbuilt types.
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.
|