关于 js Map 对象的缓存问题

简单记录一下 小程序项目中遇到的 关于 Map 的问题

现象:

  1. map 对象 初始化接口 数据
  2. 由表单操作动态更新 map values
  3. 页面销毁
  4. 重新进入此页面 -> map 被赋值了之前页面的map值(不符合预期)
  5. 操作如下  页面销毁时 清空 map Map.clear()
  6. 再次进入发现 依然会被初始化之前的值
  7. 查找mdn(https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map/clear) 无果
  8. 查找规范 (https://www.ecma-international.org/ecma-262/6.0/#sec-map.prototype.clear

下面划重点:

The following steps are taken:

  1. Let M be the this value.
  2. If Type(M) is not Object, throw a TypeError exception.
  3. If M does not have a [[MapData]] internal slot, throw a TypeError exception.
  4. Let entries be the List that is the value of M’s [[MapData]] internal slot.
  5. Repeat for each Record {[[key]], [[value]]} p that is an element of entries,
    1. Set p.[[key]] to empty.
    2. Set p.[[value]] to empty.
  6. 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()

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值