-
Notifications
You must be signed in to change notification settings - Fork 1
/
2_dot-vs-bracket.js
31 lines (20 loc) · 1011 Bytes
/
2_dot-vs-bracket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var aFalse = 'aTrue';
var a = {
aTrue: 'True',
aFalse: 'False'
};
var x = a.aTrue;
console.log(x); // -> True
var y = a.aFalse;
console.log(y); // -> False
/* let the black magic happen! */
var q = a[aFalse]; // ! invokes a value of variable declared outside of the object
console.log(q); // -> True
var z = a.aFalse; // ! invokes a value of variable declared inside of the object
console.log(z); // -> False
/* if you want to get the object values, you can use Object.values(object_name) */
Object.values(a); // -> ["True", "False"] (array of values)
/* https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Object/values */
/* it's also good to know Object.entries() - it returns an array whose elements are arrays corresponding to the enumerable property */
Object.entries(a); // -> [["aTrue", "True"], ["aFalse", "False"]] (array of arrays) :)
/* https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Object/entries */