Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WeakMaps section #62

Open
Kikobeats opened this issue Dec 24, 2017 · 0 comments
Open

Add WeakMaps section #62

Kikobeats opened this issue Dec 24, 2017 · 0 comments

Comments

@Kikobeats
Copy link
Owner

Kikobeats commented Dec 24, 2017

Reading about Proxies at
https://hacks.mozilla.org/2015/07/es6-in-depth-proxies-and-reflect/

I found a very nice WeakMaps usage example:

Proxies ♥ WeakMaps. In our readOnlyView example, we create a new proxy every time an object is accessed. It could save a lot of memory to cache every proxy we create in a WeakMap, so that however many times an object is passed to readOnlyView, only a single proxy is created for it.

  throw new Error("can't modify read-only view");
}

var handler = {
  // Override all five mutating methods.
  set: NOPE,
  defineProperty: NOPE,
  deleteProperty: NOPE,
  preventExtensions: NOPE,
  setPrototypeOf: NOPE
};

function readOnlyView(target) {
  return new Proxy(target, handler);
}

This is one of the motivating use cases for WeakMap.

Because WeakMaps is confused with Maps, could be interesting add a section about when to use WeakMaps

@Kikobeats Kikobeats changed the title Add WeakMaps example Add WeakMaps section Dec 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant