From ca08af29c8f02b7bd1ca2b1ee35f6c9bb59cbde6 Mon Sep 17 00:00:00 2001 From: Zeno Popovici Date: Fri, 1 Sep 2017 17:18:12 +0300 Subject: [PATCH] Revert "Hash maps with no side effects" --- .../2017-xx-xx-hash-maps-without-side-effects | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 _posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects diff --git a/_posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects b/_posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects deleted file mode 100644 index 8da73e4f..00000000 --- a/_posts/en/javascript/2017-xx-xx-hash-maps-without-side-effects +++ /dev/null @@ -1,49 +0,0 @@ ---- -layout: *post - -title: Hash maps without side effects -tip-number: 73 -tip-username: bhaskarmelkani -tip-username-profile: https://www.twitter.com/bhaskarmelkani -tip-tldr: Create hash maps(without side effects) using `Object.create(null)`. - -categories: - - en - - javascript ---- -When you want to use javascript object as a hash map(purely for storing data), you might want to create it as follows. - -```js - const map = Object.create(null); -``` - -When creating a map using object literal(`const map = {}`), the map inherites properties from Object by default. It is equivalent to `Object.create(Object.prototype)`. - -But by doing `Object.create(null)`, we explicitly specify `null` as its prototype. So it have absolutely no properties, not even constructor, toString, hasOwnProperty, etc. so you're free to use those keys in your data structure if you need to. - -**Rationale:-** -```js -const dirtyMap = {}; -const cleanMap = Object.create(null); - -dirtyMap.constructor // function Object() { [native code] } - -cleanMap.constructor // undefined - -//iterating maps - -const key; -for(key in dirtyMap){ - if (dirtyMap.hasOwnProperty(key)) { // Check to avoid iterating over inherited properties. - console.log(key + " -> " + dirtyMap[key]); - } -} - -for(key in cleanMap){ - console.log(key + " -> " + cleanMap[key]); // No need to add extra checks, as the object will always be clean -} -``` - -**Note**: -* Object.create() was introduced in ES5 [[Compatibility]](http://kangax.github.io/compat-table/es5/) -* ES6 introduced some new structures: [Map](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map), [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap), [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and [Weak Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet)