-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.js
More file actions
50 lines (39 loc) · 1.2 KB
/
ex1.js
File metadata and controls
50 lines (39 loc) · 1.2 KB
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
'use strict';
/*
Problem:
Implement `createHidingProxy(target, hiddenKeys)`.
Behavior:
- Hide keys in `hiddenKeys` from:
- `in` operator
- Object.keys / Reflect.ownKeys
- direct property access (return undefined)
Invariant rule:
- Non-configurable own properties cannot be hidden.
- If caller requests hiding one, throw a clear Error.
Implementation notes:
- Use Reflect operations to preserve normal behavior for non-hidden keys.
- Preserve Proxy invariants.
Starter code is intentionally incorrect:
- Hides keys blindly (including non-configurable).
- Uses direct property access in traps instead of Reflect.
*/
function createHidingProxy(target, hiddenKeys) {
if (!target || (typeof target !== 'object' && typeof target !== 'function')) {
throw new TypeError('target must be an object or function');
}
const hidden = new Set(hiddenKeys || []);
return new Proxy(target, {
get(t, prop) {
if (hidden.has(prop)) return undefined;
return t[prop];
},
has(t, prop) {
if (hidden.has(prop)) return false;
return prop in t;
},
ownKeys(t) {
return Reflect.ownKeys(t).filter((key) => !hidden.has(key));
},
});
}
module.exports = { createHidingProxy };