-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path3-properties.js
More file actions
56 lines (48 loc) · 920 Bytes
/
3-properties.js
File metadata and controls
56 lines (48 loc) · 920 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
54
55
56
'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';
{
const person = { name, city };
console.dir({ person });
}
// Dynamic field name
{
const fieldName = 'city';
const fieldValue = 'Roma';
const person = {
name: 'Marcus Aurelius',
[fieldName]: fieldValue,
};
console.dir({ person });
}
// Expression in field name
{
const prefix = 'city';
const person = {
name: 'Marcus Aurelius',
[prefix + 'Born']: 'Roma',
};
console.dir({ person });
}
// Function in field name
{
const fn = (s) => s + 'Born';
const person = {
name: 'Marcus Aurelius',
[fn('city')]: 'Roma',
};
console.dir({ person });
}