Going through the Eloquent Javascript book where I encountered something I haven't seen before.
In the code snippet below the variable 'map' is followed by empty curly braces. Can someone please explain what this means? Does this do anything tot he function that follows.
Also, can someone explain what map[event] = phi; does exactly? I take it this map refers to the variable 'map' we declared in the first line...
var map = {};
function storePhi (event, phi) {
map[event] = phi;
}
storePhi("pizza", 0.069);
Going through the Eloquent Javascript book where I encountered something I haven't seen before.
In the code snippet below the variable 'map' is followed by empty curly braces. Can someone please explain what this means? Does this do anything tot he function that follows.
Also, can someone explain what map[event] = phi; does exactly? I take it this map refers to the variable 'map' we declared in the first line...
var map = {};
function storePhi (event, phi) {
map[event] = phi;
}
storePhi("pizza", 0.069);
Share
Improve this question
asked Jun 24, 2016 at 16:23
Ronny BlomRonny Blom
1492 silver badges6 bronze badges
4
|
2 Answers
Reset to default 14The {}
represents an empty object
.
map[event] = phi
will add (or overwrite) a property on the map
object with the name event
and assign it to a value of phi
. This way, you can do map.EVENT_NAME
to get the value of phi for that event.
After performing storePhi("pizza", 0.069);
, map will look like this:
console.log(map);
map = {
pizza: 0.069
}
console.log(map.pizza);
map.pizza = 0.069
It means the variable is a dictionary that stores key value pairs. The subscript or the value within the [] brackets is the key and the value on the right side is the value.
map = {}
is declaring an object.map[event] = phi
is adding a property tomap
where the name is equal to the value ofevent
andphi
is the associated value. – Mike Cluck Commented Jun 24, 2016 at 16:24