Make object keys private/invisible via Symbols
JS
S
JavaScriptProtect object keys from tampering/hacking using Symbols. Symbols are a new, special kind of object that can be used as a unique property name in objects. https://hacks.mozilla.org/2015/06/es6-in-depth-symbols/
1// import from another module
2var type = Symbol();
3
4class Telemetry {
5 constructor(val,typ){
6 this.value = val;
7 this[type] = typ; //private
8 }
9 emit(){
10 console.log(`Telemetry type is ${this[type]}, value is ${this.value}`);
11 }
12}
13
14const tel = new Telemetry(200, 'xpto');
15console.log(tel);
16
17// --------------------------------------
18// Read type (only if the symbol is know)
19console.log(tel[type]);
20
21// Try to Hack
22console.log(JSON.stringify(tel)); // {"value": 200}
23console.log(Object.keys(tel)); Created on 12/10/2017