From a2e0ee417746eb268cf9ddc11ab3ccd71995b3c3 Mon Sep 17 00:00:00 2001 From: Nicita01 Date: Tue, 13 Mar 2018 22:09:47 +0200 Subject: [PATCH 1/2] Fix incorrect name of module --- JavaScript/4-iterator.js | 2 ++ JavaScript/6-hide-symbols.js | 1 - JavaScript/7-hide-symbols-usage.js | 18 ++++++++---------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/JavaScript/4-iterator.js b/JavaScript/4-iterator.js index 3aea803..e2c5a92 100644 --- a/JavaScript/4-iterator.js +++ b/JavaScript/4-iterator.js @@ -24,6 +24,8 @@ Object.defineProperty(generateNumbersObject, Symbol.iterator, { configurable: false }); +// Usage: + console.dir(generateNumbersObject); console.log(Object.getOwnPropertySymbols(generateNumbersObject)); diff --git a/JavaScript/6-hide-symbols.js b/JavaScript/6-hide-symbols.js index e3a8547..8a8cff3 100644 --- a/JavaScript/6-hide-symbols.js +++ b/JavaScript/6-hide-symbols.js @@ -1,4 +1,3 @@ -'use strict'; // If an ordinary user wants to overwrite a secret field, // we can not throw error or forbid it, diff --git a/JavaScript/7-hide-symbols-usage.js b/JavaScript/7-hide-symbols-usage.js index f2391a3..187d457 100644 --- a/JavaScript/7-hide-symbols-usage.js +++ b/JavaScript/7-hide-symbols-usage.js @@ -1,7 +1,10 @@ 'use strict'; -const hideSymbol = require('./7-hide-symbols.js'); +const hideSymbol = require('./6-hide-symbols.js'); +// we have added an opcional method _debugOutputSecretField, +// to check at the end of the work that the information in the secret field was left +// This is the only way to verify this information let obj = { name: 'Marcus Aurelius', born: 121, @@ -10,7 +13,10 @@ let obj = { get getter() { return 'GETTER'; }, - set setter(value) {} + set setter(value) {}, + get _debugOutputSecretField() { + return this[Symbol.for('secret')]; + } }; console.log('\n\nBEFORE PROXYING:\n\n'); @@ -49,14 +55,6 @@ console.log(obj[Symbol.for('secret')]); console.log('\x1b[4mconsole.log(Object.entries(obj)):\x1b[0m'); console.log(Object.entries(obj)); -// we have added an opcional method _debugOutputSecretField, -// to check at the end of the work that the information in the secret field was left -// This is the only way to verify this information -Object.defineProperty(obj, '_debugOutputSecretField', { - enumerable: false, - get: () => this[Symbol.for('secret')], - configurable: true -}); // proxying: obj = hideSymbol(obj, Symbol.for('secret')); From 83e9253f4a7b821b83003a2cc07e11bc1e8d81d4 Mon Sep 17 00:00:00 2001 From: Nicita01 Date: Tue, 13 Mar 2018 23:38:52 +0200 Subject: [PATCH 2/2] Joining tests to the main file --- JavaScript/6-hide-symbols.js | 126 ++++++++++++++++++++++++++++- JavaScript/7-hide-symbols-usage.js | 114 -------------------------- 2 files changed, 125 insertions(+), 115 deletions(-) delete mode 100644 JavaScript/7-hide-symbols-usage.js diff --git a/JavaScript/6-hide-symbols.js b/JavaScript/6-hide-symbols.js index 8a8cff3..b462e31 100644 --- a/JavaScript/6-hide-symbols.js +++ b/JavaScript/6-hide-symbols.js @@ -1,3 +1,4 @@ +'use strict'; // If an ordinary user wants to overwrite a secret field, // we can not throw error or forbid it, @@ -11,6 +12,10 @@ function hideSymbol(obj, symbol) { realObj: Object.assign(obj), simulateSecretField: undefined }; + + // As a result, the field obj.simulateSecretField in the object itself remained free + // And the user can use it as a normal fieldw + obj = new Proxy(obj, { ownKeys: (target) => { if (symbol in target.realObj) { @@ -70,5 +75,124 @@ function hideSymbol(obj, symbol) { return obj; } -module.exports = hideSymbol; +// Usage: + +// we have added an opcional method _debugOutputSecretField, +// to check at the end of the work that the information in the secret field was left +// This is the only way to verify this information +// (This field should not be in a real project) + +let obj = { + name: 'Marcus Aurelius', + born: 121, + [Symbol.for('secret')]: 'some secret information', + [Symbol.for('notsecret')]: 'some not secret information', + get getter() { + return 'GETTER'; + }, + set setter(value) {}, + get _debugOutputSecretField() { + return this[Symbol.for('secret')]; + } +}; + +// We must completely hide the field obj[Symbol.for('secret')] + +console.log('\n\nBEFORE PROXYING:\n\n'); + +console.log('\x1b[4mfor in:\x1b[0m'); +for (const i in obj) { + console.log(i); +} + +console.log('\x1b[4mconsole.log("obj"):\x1b[0m'); +console.log(obj); + +console.log('\x1b[4mconsole.dir("obj"):\x1b[0m'); +console.dir(obj, { + showHidden: true, + depth: null +}); + +console.log('\x1b[4mconsole.log(Object.getOwnPropertyNames):\x1b[0m'); +console.log(Object.getOwnPropertyNames(obj)); + +console.log('\x1b[4mconsole.log(Object.getOwnPropertySymbols):\x1b[0m'); +console.log(Object.getOwnPropertySymbols(obj)); + +console.log( + '\x1b[4mconsole.log(Object.getOwnPropertyDescriptor(obj, Symbol.for("secret")))\x1b[0m' +); +console.log(Object.getOwnPropertyDescriptor(obj, Symbol.for('secret'))); + +console.log('\x1b[4mconsole.log(Object.keys(obj)):\x1b[0m'); +console.log(Object.keys(obj)); + +console.log('\x1b[4mconsole.log(obj[Symbol.for("secret")]):\x1b[0m'); +console.log(obj[Symbol.for('secret')]); + +console.log('\x1b[4mconsole.log(Object.entries(obj)):\x1b[0m'); +console.log(Object.entries(obj)); + + +// proxying: +obj = hideSymbol(obj, Symbol.for('secret')); + +console.log('\n\nAFTER PROXYING:\n\n'); + +console.log('\x1b[4mfor in:\x1b[0m'); +for (const i in obj) { + console.log(i); +} + +console.log('\x1b[4mconsole.log("obj"):\x1b[0m'); +console.log(obj); + +console.log('\x1b[4mconsole.dir("obj"):\x1b[0m'); +console.dir(obj, { + showHidden: true, + depth: null +}); + +console.log('\x1b[4mconsole.log(Object.getOwnPropertyNames):\x1b[0m'); +console.log(Object.getOwnPropertyNames(obj)); + +console.log('\x1b[4mconsole.log(Object.getOwnPropertySymbols):\x1b[0m'); +console.log(Object.getOwnPropertySymbols(obj)); + +console.log( + '\x1b[4mconsole.log(Object.getOwnPropertyDescriptor(obj, Symbol.for("secret")))\x1b[0m' +); +console.log(Object.getOwnPropertyDescriptor(obj, Symbol.for('secret'))); + +console.log('\x1b[4mconsole.log(Object.keys(obj)):\x1b[0m'); +console.log(Object.keys(obj)); + +console.log('\x1b[4mconsole.log(obj[Symbol.for("secret")])\x1b[0m'); +console.log(obj[Symbol.for('secret')]); + +console.log('\x1b[4mconsole.log(Object.entries(obj)):\x1b[0m'); +console.log(Object.entries(obj)); + +// overwrite: + +console.log('\n\nOVERWRITE SECRET FIELD:\n\n'); + +console.log('\x1b[4m[Symbol.for("secret")] = "overwrite value"\x1b[0m'); +obj[Symbol.for('secret')] = 'overwrite value'; + +console.log('\x1b[4mconsole.log("obj"):\x1b[0m'); +console.log(obj); + +console.log('\x1b[4mconsole.log(Object.getOwnPropertySymbols):\x1b[0m'); +console.log(Object.getOwnPropertySymbols(obj)); + +console.log('\x1b[4mconsole.log(obj[Symbol.for("secret")])\x1b[0m'); +console.log(obj[Symbol.for('secret')]); + +// in fact, in the secret field our line is still stored + +console.log('\n\nREAL VALUE OF obj[Symbol.for("secret")]:\n\n'); +console.log('\x1b[4mconsole.log(obj._debugOutputSecretField):\x1b[0m'); +console.log(obj._debugOutputSecretField); diff --git a/JavaScript/7-hide-symbols-usage.js b/JavaScript/7-hide-symbols-usage.js deleted file mode 100644 index 187d457..0000000 --- a/JavaScript/7-hide-symbols-usage.js +++ /dev/null @@ -1,114 +0,0 @@ -'use strict'; - -const hideSymbol = require('./6-hide-symbols.js'); - -// we have added an opcional method _debugOutputSecretField, -// to check at the end of the work that the information in the secret field was left -// This is the only way to verify this information -let obj = { - name: 'Marcus Aurelius', - born: 121, - [Symbol.for('secret')]: 'some secret information', - [Symbol.for('notsecret')]: 'some not secret information', - get getter() { - return 'GETTER'; - }, - set setter(value) {}, - get _debugOutputSecretField() { - return this[Symbol.for('secret')]; - } -}; - -console.log('\n\nBEFORE PROXYING:\n\n'); - -console.log('\x1b[4mfor in:\x1b[0m'); -for (const i in obj) { - console.log(i); -} - -console.log('\x1b[4mconsole.log("obj"):\x1b[0m'); -console.log(obj); - -console.log('\x1b[4mconsole.dir("obj"):\x1b[0m'); -console.dir(obj, { - showHidden: true, - depth: null -}); - -console.log('\x1b[4mObject.getOwnPropertyNames:\x1b[0m'); -console.log(Object.getOwnPropertyNames(obj)); - -console.log('\x1b[4mObject.getOwnPropertySymbols:\x1b[0m'); -console.log(Object.getOwnPropertySymbols(obj)); - -console.log( - '\x1b[4mconsole.log(Object.getOwnPropertyDescriptor(obj, Symbol.for("secret")))\x1b[0m' -); -console.log(Object.getOwnPropertyDescriptor(obj, Symbol.for('secret'))); - -console.log('\x1b[4mconsole.log(Object.keys(obj)):\x1b[0m'); -console.log(Object.keys(obj)); - -console.log('\x1b[4mconsole.log(obj[Symbol.for("secret")])\x1b[0m'); -console.log(obj[Symbol.for('secret')]); - -console.log('\x1b[4mconsole.log(Object.entries(obj)):\x1b[0m'); -console.log(Object.entries(obj)); - - -// proxying: -obj = hideSymbol(obj, Symbol.for('secret')); - -console.log('\n\nAFTER PROXYING:\n\n'); - -console.log('\x1b[4mfor in:\x1b[0m'); -for (const i in obj) { - console.log(i); -} - -console.log('\x1b[4mconsole.log("obj"):\x1b[0m'); -console.log(obj); - -console.log('\x1b[4mconsole.dir("obj"):\x1b[0m'); -console.dir(obj, { - showHidden: true, - depth: null -}); - -console.log('\x1b[4mObject.getOwnPropertyNames:\x1b[0m'); -console.log(Object.getOwnPropertyNames(obj)); - -console.log('\x1b[4mObject.getOwnPropertySymbols:\x1b[0m'); -console.log(Object.getOwnPropertySymbols(obj)); - -console.log( - '\x1b[4mconsole.log(Object.getOwnPropertyDescriptor(obj, Symbol.for("secret")))\x1b[0m' -); -console.log(Object.getOwnPropertyDescriptor(obj, Symbol.for('secret'))); - -console.log('\x1b[4mconsole.log(Object.keys(obj)):\x1b[0m'); -console.log(Object.keys(obj)); - -console.log('\x1b[4mconsole.log(obj[Symbol.for("secret")])\x1b[0m'); -console.log(obj[Symbol.for('secret')]); - -console.log('\x1b[4mconsole.log(Object.entries(obj)):\x1b[0m'); -console.log(Object.entries(obj)); - -// overwrite: -console.log('\n\x1b[4m[Symbol.for("secret")] = "overwrite value"\x1b[0m'); -obj[Symbol.for('secret')] = 'overwrite value'; - -console.log('\x1b[4mconsole.log("obj"):\x1b[0m'); -console.log(obj); - -console.log('\x1b[4mObject.getOwnPropertySymbols:\x1b[0m'); -console.log(Object.getOwnPropertySymbols(obj)); - -console.log('\x1b[4mconsole.log(obj[Symbol.for("secret")])\x1b[0m'); -console.log(obj[Symbol.for('secret')]); - -// in fact, in the secret field our line is still stored -console.log('\n\nREAL VALUE OF obj[Symbol.for("secret")]:\n\n'); -console.log('\x1b[4mconsole.log(obj._debugOutputSecretField):\x1b[0m'); -console.log(obj._debugOutputSecretField);