Is it possible to create a “weak reference” in javascript?

前端 未结 9 1686
忘掉有多难
忘掉有多难 2020-11-28 04:53

Is there any way in javascript to create a \"weak reference\" to another object? Here is the wiki page describing what a weak reference is. Here is another article that desc

相关标签:
9条回答
  • 2020-11-28 05:29

    Just for reference; JavaScript doesn't have it, but ActionScript 3 (which is also ECMAScript) does. Check out the constructor parameter for Dictionary.

    0 讨论(0)
  • 2020-11-28 05:30

    There is no language support for weakrefs in JavaScript. You can roll your own using manual reference counting, but not especially smoothly. You can't make a proxy wrapper object, because in JavaScript objects never know when they're about to be garbage-collected.

    So your ‘weak reference’ becomes a key (eg. integer) in a simple lookup, with an add-reference and remove-reference method, and when there are no manually-tracked references anymore then entry can be deleted, leaving future lookups on that key to return null.

    This is not really a weakref, but it can solve some of the same problems. It's typically done in complex web applications to prevent memory leakage from browsers (typically IE, especially older versions) when there is a reference loop between a DOM Node or event handler, and an object associated with it such as a closure. In these cases a full reference-counting scheme may not even be necessary.

    0 讨论(0)
  • 2020-11-28 05:31

    http://www.jibbering.com/faq/faq_notes/closures.html

    ECMAScript uses automatic garbage collection. The specification does not define the details, leaving that to the implementers to sort out, and some implementations are known to give a very low priority to their garbage collection operations. But the general idea is that if an object becomes un-referable (by having no remaining references to it left accessible to executing code) it becomes available for garbage collection and will at some future point be destroyed and any resources it is consuming freed and returned to the system for re-use.

    This would normally be the case upon exiting an execution context. The scope chain structure, the Activation/Variable object and any objects created within the execution context, including function objects, would no longer be accessible and so would become available for garbage collection.

    Meaning there are no weak ones only ones that no longer become available.

    0 讨论(0)
  • 2020-11-28 05:41

    EcmaScript 6 (ES Harmony) has a WeakMap object. Browser support amongst modern browsers is pretty good (the last 3 versions of Firefox, chrome and even an upcoming IE version support it).

    0 讨论(0)
  • 2020-11-28 05:43

    True weak references, no, not yet (but browser makers are looking at the subject). But here is an idea on how to simulate weak references.

    You could build a cache which you drive your objects through. When an object is stored, the cache keeps a prediction of how much memory the object will take up. For some items, like storing images, this is straight forward to work out. For others this would be more difficult.

    When you need an object, you then ask the cache for it. If the cache has the object, it is returned. If it is not there, then the item is generated, stored, and then returned.

    The weak references are simulated by the cache removing items, when the total amount of predicted memory reaches a certain level. It will predict which items are least used based on how often they are retrieved, weighted by how long ago they were taken out. A 'calculation' cost could also be added, if the code that creates the item is passed into the cache as a closure. This would allow the cache to keep items which are very expensive to build or generate.

    The deletion algorithm is key, because if you get this wrong then you could end up removing the most popular items. This would cause terrible performance.

    As long as the cache is the only object with permanent references to the objects stored, then the above system should work pretty well as an alternative to true weak references.

    0 讨论(0)
  • 2020-11-28 05:46

    Finally they are here. Not yet implemented in browsers, but soon to be.

    https://v8.dev/features/weak-references

    0 讨论(0)
提交回复
热议问题