javascript - 无法更新 Set 属性

标签 javascript immutable.js

var npm = require("npm")
var immutable = require("immutable");
var test = immutable.fromJS

const a = test({name:true,b:[]})
console.log(a);
a.set('name',false);

console.log("------------");
console.log(a.get('name')) // gives still value true.

最后一个控制台的期望值是多少?我认为这是真的。有人可以帮助我哪里出错了

最佳答案

Immutable.js 的要点是您从中获取的对象是不可变的(无法更改)。看起来像增变器操作的操作返回一个新对象。所以:

a = a.set('name', false);

来自 the documentation :

set()

Returns a new Map also containing the new key, value pair. If an equivalent key already exists in this Map, it will be replaced.

(我的重点)

关于javascript - 无法更新 Set 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54652484/

相关文章:

javascript - 强制子类不可变

javascript - 在 Immutable.js 中扩展记录

javascript - ImmutableJS fromJS() 和 Map() "ownerID"不匹配

javascript - "push-model"在 RxJS/Immutable.js/AngularJS 等上下文中的含义

javascript - 用于将鼠标悬停在元素上的 Angularjs 指令

javascript - 如何获取具有填充的div内文本的位置

javascript - AngularJS - 如何恢复选择中的选定选项

javascript - 如何在 asp.net 中使用 jquery 将 json 数据显示为 html <ul><li></li></ul> 标记

javascript - 在页面中嵌入外部网站

javascript - Redux 和 immutable js,我在尝试使用 immutable 后失去了状态