26.1 WeakRef Objects
A
26.1.1 The WeakRef Constructor
The WeakRef
- is %WeakRef%.
-
is the initial value of the
"WeakRef" property of theglobal object . -
creates and initializes a new WeakRef when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
-
may be used as the value in an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specifiedWeakRef
behaviour must include asuper
call to theWeakRef
constructor to create and initialize the subclass instance with the internal state necessary to support theWeakRef.prototype
built-in methods.
26.1.1.1 WeakRef ( target )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - If
CanBeHeldWeakly (target) isfalse , throw aTypeError exception. - Let weakRef be ?
OrdinaryCreateFromConstructor (NewTarget,"%WeakRef.prototype%" , « [[WeakRefTarget]] »). - Perform
AddToKeptObjects (target). - Set weakRef.[[WeakRefTarget]] to target.
- Return weakRef.
26.1.2 Properties of the WeakRef Constructor
The
-
has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
26.1.2.1 WeakRef.prototype
The initial value of WeakRef.prototype
is the
This property has the attributes { [[Writable]]:
26.1.3 Properties of the WeakRef Prototype Object
The WeakRef prototype object:
- is %WeakRef.prototype%.
-
has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[WeakRefTarget]] internal slot.
26.1.3.1 WeakRef.prototype.constructor
The initial value of WeakRef.prototype.constructor
is
This property has the attributes { [[Writable]]:
26.1.3.2 WeakRef.prototype.deref ( )
This method performs the following steps when called:
- Let weakRef be the
this value. - Perform ?
RequireInternalSlot (weakRef, [[WeakRefTarget]]). - Return
WeakRefDeref (weakRef).
If the
let target = { foo() {} };
let weakRef = new WeakRef(target);
// ... later ...
if (weakRef.deref()) {
weakRef.deref().foo();
}
In the above example, if the first deref does not evaluate to
26.1.3.3 WeakRef.prototype [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]:
26.1.4 WeakRef Abstract Operations
26.1.4.1 WeakRefDeref ( weakRef )
The abstract operation WeakRefDeref takes argument weakRef (a
- Let target be weakRef.[[WeakRefTarget]].
- If target is not
empty , then- Perform
AddToKeptObjects (target). - Return target.
- Perform
- Return
undefined .
This abstract operation is defined separately from WeakRef.prototype.deref strictly to make it possible to succinctly define liveness.