@todojs/

Proxy 6 - hidden()

JavaScript

No description

fork
loading
main.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
32
33
34
var obj = {
    a: 1, b: 2, c: 3
};

console.log('obj =', obj);
console.log('getPropertyNames(obj) =', getPropertyNames(obj));

var objHidden = hidden(obj);
console.log('objHidden =', objHidden);
console.log('objHidden.a =', objHidden.a);
console.log('objHidden.b =', objHidden.b);
console.log('objHidden.c =', objHidden.c);
console.log('getPropertyNames(objHidden) =', getPropertyNames(objHidden));

function hidden(obj) {
    var proto = Object.getPrototypeOf(obj);
    if (typeof proto === 'object' && proto !== null) {
        Object.setPrototypeOf(obj, hidden(proto));
    }
    return new Proxy(obj, {
        has: () => false,
        ownKeys: () => []
    });
}

function getPropertyNames(obj) {
    var proto = Object.getPrototypeOf(obj);
    return (
        (typeof proto === 'object' && proto !== null ? getPropertyNames(proto) : [])
            .concat(Object.getOwnPropertyNames(obj))
            .filter(function(item, pos, result) { return result.indexOf(item) === pos; })
            .sort()
    );
}
Native Browser JavaScript