-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path4-has.js
More file actions
35 lines (31 loc) · 751 Bytes
/
4-has.js
File metadata and controls
35 lines (31 loc) · 751 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
'use strict';
const data = { name: 'Marcus Aurelius', city: 'Rome', born: 121 };
const person = new Proxy(data, {
has(obj, key) {
console.log('check', key);
return key in obj || key === 'age';
},
get(obj, key) {
console.log('get', key);
if (key === 'age') {
const current = new Date().getFullYear();
const year = new Date(obj.born.toString()).getFullYear();
return current - year;
}
return obj[key];
},
});
console.log('Try "age" in person');
if ('age' in person) {
console.log('Try person.age');
if (person.age) {
console.log('Try person["age"]');
if (person['age']) {
console.log({
born: person.born,
age: person.age,
});
}
}
}
console.dir(person);