How to iterate over a weakmap?

房东的猫 提交于 2019-12-18 05:39:04

问题


A javascript WeakMap ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap ) does not allow you to get the key, or the length or size, by design.

Is it possible to nevertheless loop over entries in some way ?

If not .. how does the Chrome console do this ?


回答1:


Is it possible to nevertheless loop over entries in some way?

No, as you say, the contents of a WeakMap are not accessible by design, and there is no iterability.

If not … how does the Chrome console do this?

The console uses the debugging API of the JS engine, which allows access to the internals of objects (also to promise states, wrapped primitives, etc.) and many more.




回答2:


Things are moving and it will soon be possible to create iterable week maps thanks to weak refs. See the iterable WeakMap example in the tc39 weakrefs proposal.

(note that it is already possible with nodejs v12.?.? using --harmony-weak-refs flag)



来源:https://stackoverflow.com/questions/32402837/how-to-iterate-over-a-weakmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!