专业网站优化外包,.net 做网站,小企业建站系统,哪一个平阳网站建设ES12 (ECMAScript 2021) 特性总结#xff1a;WeakRef
1. WeakRef 概述
描述
WeakRef 是 ES12 引入的一个新特性#xff0c;用于创建对对象的弱引用。弱引用不会阻止垃圾回收器回收对象#xff0c;即使该对象仍然被弱引用持有。WeakRef 通常与 FinalizationRegistry 结合使…ES12 (ECMAScript 2021) 特性总结WeakRef
1. WeakRef 概述
描述
WeakRef 是 ES12 引入的一个新特性用于创建对对象的弱引用。弱引用不会阻止垃圾回收器回收对象即使该对象仍然被弱引用持有。WeakRef 通常与 FinalizationRegistry 结合使用用于在对象被垃圾回收时执行清理操作。
语法
const weakRef new WeakRef(targetObject);主要方法
deref()返回弱引用指向的目标对象。如果目标对象已被垃圾回收则返回 undefined。 2. WeakRef 的用法
创建弱引用
const obj { name: Alice };
const weakRef new WeakRef(obj);// 访问目标对象
const target weakRef.deref();
console.log(target); // { name: Alice }检查对象是否被回收
let obj { name: Bob };
const weakRef new WeakRef(obj);// 解除对 obj 的强引用
obj null;// 强制触发垃圾回收仅用于演示实际环境中不应手动调用
global.gc();// 检查对象是否被回收
setTimeout(() {const target weakRef.deref();console.log(target); // undefined对象已被回收
}, 1000);3. WeakRef 的使用场景
1. 缓存系统
在缓存系统中可以使用 WeakRef 来缓存对象。当内存不足时垃圾回收器会自动回收这些对象从而避免内存泄漏。
示例
const cache new Map();function getCachedData(key) {let cachedRef cache.get(key);if (cachedRef) {const cachedData cachedRef.deref();if (cachedData) {return cachedData;}}// 重新获取数据并缓存const newData fetchData(key);cache.set(key, new WeakRef(newData));return newData;
}2. 监听对象生命周期
结合 FinalizationRegistry可以在对象被垃圾回收时执行清理操作。
示例
const registry new FinalizationRegistry((heldValue) {console.log(Object with value ${heldValue} has been garbage collected.);
});let obj { name: Charlie };
const weakRef new WeakRef(obj);// 注册清理回调
registry.register(obj, some metadata);// 解除对 obj 的强引用
obj null;// 当 obj 被垃圾回收时会触发清理回调3. 避免内存泄漏
在需要持有对象引用但又不想阻止垃圾回收的场景中WeakRef 是一个理想的选择。
示例
class EventListener {constructor(target) {this.targetRef new WeakRef(target);this.handleEvent this.handleEvent.bind(this);target.addEventListener(click, this.handleEvent);}handleEvent(event) {const target this.targetRef.deref();if (target) {console.log(Event triggered on:, target);} else {// 目标对象已被回收清理事件监听器event.currentTarget.removeEventListener(click, this.handleEvent);}}
}4. 注意事项 垃圾回收的不确定性 垃圾回收的时机由 JavaScript 引擎决定无法手动控制。即使对象不再被强引用垃圾回收也可能不会立即执行。 避免滥用 WeakRef 和 FinalizationRegistry 是高级特性通常只在特定场景下使用。过度使用可能导致代码难以理解和维护。 兼容性 确保目标运行环境支持 WeakRef 和 FinalizationRegistry。 5. 总结
特性描述使用场景WeakRef创建对对象的弱引用不会阻止垃圾回收器回收对象缓存系统、监听对象生命周期、避免内存泄漏deref()返回弱引用指向的目标对象若对象已被回收则返回 undefined检查对象是否仍然可用结合 FinalizationRegistry在对象被垃圾回收时执行清理操作对象生命周期管理、资源清理