Skip to content

Instantly share code, notes, and snippets.

@oliverjumpertz
Created December 24, 2020 20:40
Show Gist options
  • Save oliverjumpertz/6db42f594201d4d945428879fb362afc to your computer and use it in GitHub Desktop.
Save oliverjumpertz/6db42f594201d4d945428879fb362afc to your computer and use it in GitHub Desktop.
Creating a vanilla dictionary
const dict = {};
dict.__proto__; // => {}
console.log(dict.hasOwnProperty); // => f hasOwnProperty() {}
const emptyDict = Object.create(null);
emptyDict.__proto__; // => undefined
emptyDict.hasOwnProperty; // => undefined
// ❗️ Anyone can modify the object prototype from the outside
Object.prototype.myFunction = function () {};
// ❌ As you can see here, dict was affected.
dict.myFunction; // => f () {}
// ✅ But our emptyDict remains unaffected!
emptyDict.myFunction; // => undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment