- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将我的类属性设置为私有(private),所以我使用了 WeakMap,但使用了一个 WeakMap。创建项目后,我只得到最后一个对象数据,以前的数据被删除......
这是我的代码定义:
const Item = (() => {
const weakMap = new WeakMap();
const _id = {};
const _text = {};
class Item {
constructor({id, text}) {
weakMap.set(_id, {id});
weakMap.set(_text, {text});
}
getId() {
return weakMap.get(_id).id;
}
getText() {
return weakMap.get(_text).text;
}
}
return Item;
})();
const items = myItems.map(item => {
const newItem = new Item(item);
console.log('new item created, id: ' + newItem.getId());
return newItem;
});
new item created, id: 1
new item created, id: 2
new item created, id: 3
new item created, id: 4
items.forEach(element => console.log(element.getId() + ' ' + element.getText()));
4 Fourth description
4 Fourth description
4 Fourth description
4 Fourth description
const Answer = (() => {
const weakMap = new WeakMap();
const _id = {};
const _text = {};
class Answer {
getId;
getText;
constructor({id, text}) {
weakMap.set(_id, {id});
weakMap.set(_text, {text});
this.getId = () => weakMap.get(_id).id;
this.getText = () => weakMap.get(_text).text;
}
}
}
最佳答案
如果您使用 WeakMap
要实现私有(private)属性,映射中的键通常是对象/实例本身。该值是一个包含所有私有(private)属性的对象。
const Item = (() => {
const weakMap = new WeakMap();
class Item {
constructor({id, text}) {
const privateData = {id, text};
weakMap.set(this, privateData);
}
getId() {
return weakMap.get(this).id;
}
getText() {
return weakMap.get(this).text;
}
}
return Item;
})();
关于javascript - 带有 WeakMap 的 JavaScript 中的多个私有(private)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60508396/
我一直在阅读有关 WeakMap 的 MDN 文档。它提到了语法: new WeakMap([iterable]) 但是当我尝试这个时,发生了错误: var arr = [{a:1}]; var wm
这个问题在这里已经有了答案: JavaScript(ES6) WeakMap garbage collection when set an object to null (2 个答案) 关闭 4 年
我在使用 javascript 处理类的私有(private)成员时遇到了一个问题,我自己无法解决。 "use strict"; var privateData = new WeakMap(); cl
class Cat { storage = new Map() constructor(id) { if(storage.has(id)) return storage.get(id
JavaScript WeakMap不允许您通过设计获得 key 、长度或大小。 是否有可能以某种方式循环访问条目? 如果不是.. Chrome 控制台是如何做到这一点的? 最佳答案 Is it po
我正在构建 Ionic 应用程序并且一切正常,但是当我将我的源代码克隆到另一台机器时,出现如图所示的错误。 虽然应用程序在第一台机器上编译没有错误,但我检查了 Ionic、npm、typescript
标准是这样说的,毫无疑问,原始类型不能用作 WeakMap 中的键。 ;但什么是 技术原因添加这样的限制? 我想实现一个简单的 hash将 JS 值映射到 0-65535 的函数,如果使用 WeakM
为什么在 WeakMap 中不能直接访问项目?当垃圾收集器删除作为 WeakMap 键的链接时,与此键相关联的值也会被删除吗? 最佳答案 通过“直接访问项目”,我假设您的意思是为什么它们不可枚举?这是
这个问题在这里已经有了答案: How to iterate over a WeakMap? (3 个答案) Why will ES6 WeakMap's not be enumerable? (2
我的父/子关系如下所示。 parent.children = [child]; child.parent = parent; 当删除对 parent 和 child 的所有其他引用时,这是否会导致内存
我在我的 VS 中使用 typescript 1.6,当我尝试使用 WeakMap 时,我发现 Typescript 无法识别名称 WeakMap。 var _mdSideNav: typeof
ECMAScript 6中引入的WeakMap数据结构的实际用途是什么? 由于弱映射的键创建了对其对应值的强引用,因此确保已插入弱映射的值永远不会消失,只要其键还活着,它不能用于备忘录表、缓存或您通常
WeakMap polyfill尝试在不可扩展对象上定义属性时抛出错误。这些位于一堆节点、javascript 代码和库的中间,因此我实际上无法指出导致问题的原因。还有许多其他库也有自己的 polyf
我正在尝试基于 Angular 2 + Universal & ASP.NET Core Advanced Starter 构建门户 不幸的是,这个解决方案包含内置的“WeakMap”集合,即 com
我遇到了JavaScript library实现跨浏览器WeakMap在ES5中。 (WeakMap 预计用于 ES6。) 如果没有 JavaScript 语言本身的支持,这怎么可能工作? 编辑:需要
在我重新进入 JavaScript(及相关)之前,我已经完成了大量的 ActionScript 3,其中有一个带有弱键的 Dictionary 对象,就像即将推出的 WeakMap 一样;但 AS3
我有一个 memoized fn,其中 fn 由两个输入引用内存: let NewRefCursor = memoized( (deref, swap) => refToHash(deref)
我正在探索新的 ES6 功能:WeakMap。我从在 MDN 上找到的基本示例开始。 我遇到的问题是无法循环 WeakMap 元素。我收到错误: Uncaught TypeError: weakMap
我遇到了JavaScript library实现跨浏览器WeakMap在ES5中。 (WeakMap 预计用于 ES6。) 如果没有 JavaScript 语言本身的支持,这怎么可能工作? 编辑:需要
我正在使用 JavaScript weakmaps,在 google chrome 开发者控制台中尝试这段代码后,使用 --js-flags="--expose-gc"运行,我不明白为什么 weakm
我是一名优秀的程序员,十分优秀!