Skip to content

Instantly share code, notes, and snippets.

@developit
Last active February 22, 2019 21:03
Show Gist options
  • Save developit/d8d6bb56599f537aa6853fc91d9a0f70 to your computer and use it in GitHub Desktop.
Save developit/d8d6bb56599f537aa6853fc91d9a0f70 to your computer and use it in GitHub Desktop.
if (typeof WeakMap !== 'function') {
let c = 0;
WeakMap = function() {
let id = typeof Symbol === 'function' ? Symbol() : `__weak$${++c}`;
this.set = (key, val) => { key[id] = val };
this.get = key => key[id];
};
}
@developit
Copy link
Author

developit commented Feb 22, 2019

function createMap() {
  if (typeof Map=='function') return new Map();
  const keys=[], values=[];
  return {
    get(key) {
      const i = keys.indexOf(key);
      if (i!==-1) return values[i];
    },
    set(key, value) {
      const i = keys.indexOf(key);
      if (i!==-1) values[i] = value;
      else values[keys.push(key)] = value
    }
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment