forked from HowProgrammingWorks/DataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-properties.js
More file actions
53 lines (43 loc) · 809 Bytes
/
3-properties.js
File metadata and controls
53 lines (43 loc) · 809 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const person = {
name: 'Marcus',
city: 'Roma',
born: 121
};
if ('name' in person) {
console.log('Person name is: ' + person.name);
}
for (const key in person) {
const value = person[key];
console.dir({ key, value });
}
// Variables to hash
const name = 'Marcus Aurelius';
const city = 'Rome';
// Old style
const a = {
name,
city
};
// New style
const b = { name, city };
// Dynamic field name
const fieldName = 'city';
const fieldValue = 'Roma';
const person2 = {
name: 'Marcus Aurelius',
[fieldName]: fieldValue
};
// Expression in field name
const person3 = {
name: 'Marcus Aurelius',
['city' + 'Born']: fieldValue
};
// Function in field name
function fn(s) {
return s + 'Born';
}
const person4 = {
name: 'Marcus Aurelius',
[fn('city')]: fieldValue
};