简单记录一下 小程序项目中遇到的 关于 Map 的问题
现象:
- map 对象 初始化接口 数据
- 由表单操作动态更新 map values
- 页面销毁
- 重新进入此页面 -> map 被赋值了之前页面的map值(不符合预期)
- 操作如下 页面销毁时 清空 map Map.clear()
- 再次进入发现 依然会被初始化之前的值
- 查找mdn(https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/clear) 无果
- 查找规范 (https://www.ecma-international.org/ecma-262/6.0/#sec-map.prototype.clear)
下面划重点:
The following steps are taken:
- Let M be the this value.
- If Type(M) is not Object, throw a TypeError exception.
- If M does not have a [[MapData]] internal slot, throw a TypeError exception.
- Let entries be the List that is the value of M’s [[MapData]] internal slot.
- Repeat for each Record {[[key]], [[value]]} p that is an element of entries,
- Set p.[[key]] to empty.
- Set p.[[value]] to empty.
- Return undefined.
NOTE: The existing [[MapData]] List is preserved because there may be existing MapIterator objects that are suspended midway through iterating over that List.
自行理解 我的翻译水平也很蹩脚
解决方案: 在适当的时候(如:生命周期的destory) 清空/初始化你需要的值 e.g. 'value' 然后再用map.clear()