Repository navigation
Expand file tree
/
Copy path3-private-methods.js
More file actions
100 lines (92 loc) · 2.95 KB
/
Copy path3-private-methods.js
File metadata and controls
100 lines (92 loc) · 2.95 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Function used to achieve true privacy JavaScript object properties.
*
* @param {Object} target Target object to blackbox properties on.
* @param {Function} filter Function that mark the private properties.
*
* @return {Proxy} Proxy object.
*/
const
blackbox = (target, filter) => {
const handler = {
/**
* Property value getter trap that prevents returning
* private property value.
*
* @param {Object} target Target object to blackbox properties on.
* @param {String} prop Property name.
*
* @return {*} The property value.
*
* @notes
* [1] : Checks if the property isn't private.
* [2] : Bind the returned value if it's a function to the target object itself.
*/
get(target, prop) {
// [1]
if (!filter(prop)) {
let
val = Reflect.get(target, prop);
if (typeof val !== 'function')
return val;
return val.bind(target) // [2]
} else {
throw new Error(`"${prop}" property is blackboxed.`)
}
}
/**
* Property value setter trap that prevent
* assigning values to private properties.
*
* @param {Object} target Target object to blackbox properties on.
* @param {String} prop Property name.
* @param {*} val Property value.
*
* @return {void}
*/
set(target, prop, val) {
if (filter(prop))
throw new Error(`"${prop}" property is blackboxed.`);
return Reflect.set(target, prop, val)
},
/**
* Enumeration tarp that prevent the private properties from
* being enumerated.
*
* @param {Object} target Target object to blackbox properties on.
* @param {String} prop Property name.
*
* @return {Boolean}
* false if the property is private
*/
has(target, prop) {
return filter(prop) ? false : Reflect.has(target, prop)
}
/**
* Prevents private properties from being accessed via
* methods that get object keys like `Object.keys()`.
*
* @param {Object} target Target object to blackbox properties on.
*
* @return {Array}
* Array of all keys minus the private properties.
*/
ownKeys(target) {
return Reflect.ownKeys(target).filter(key => !filter(key))
}
/**
* Prevents private properties from being
* accessed via `Object.getOwnPropertyDescriptor()`.
*
* @param {Object} target Target object to blackbox properties on.
* @param {String} prop Property name.
*
* @return {Object|undefined}
* `undefined` in case private properties
*/
getOwnPropertyDescriptor(target, prop) {
return filter(prop) ? undefined : Reflect.getOwnPropertyDescriptor(target, prop);
}
};
return new Proxy(target, handler)
};