Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding this JS WeakRef polyfill implementation

I've came across this WeakRef polyfill: ungap / weakrefs.

I'm struggling to understand how it works, particularly at line 11-14:

var wr = new WeakMap;
function WeakRef(value) {
    wr.set(this, value);
}

So there is a global WeakMap wr.

And a constructor-like function WeakRef that takes a value and stores it in wr, using this (the resulting object) as key.

My understanding is the wr global WeakMap wouldn't drop the inserted value until the key is dropped. In this case, the key is the resulting WeakRef object.

So value wouldn't be allowed to be GC-ed until the created WeakRef is dropped.

Wouldn't this make the polyfill a strong reference?

like image 784
wisha Avatar asked Oct 29 '25 04:10

wisha


1 Answers

It really is strong reference.

Why couldn't they just mention that in the README. Or maybe atleast leave the issue mentioning this open??

like image 139
wisha Avatar answered Oct 30 '25 19:10

wisha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!