Javascript set操作在做什么?

Javascript set操作在做什么?,javascript,immutable.js,Javascript,Immutable.js,从下面的例子来看,我显然做错了什么 我正在尝试将node2.data(在新树中)更新为等于9。但是结果是node1.data==9(在新树中),并且node1.next的内容丢失 为什么? constnode2={data:2,next:[]}; const node1={data:1,next:[node2]}; const head1=不可变的.fromJS(node1); const head2=head1.get('next').get(0).set('data',9); consol

从下面的例子来看,我显然做错了什么

我正在尝试将
node2.data
(在新树中)更新为等于
9
。但是结果是
node1.data==9
(在新树中),并且
node1.next
的内容丢失

为什么?

constnode2={data:2,next:[]};
const node1={data:1,next:[node2]};
const head1=不可变的.fromJS(node1);
const head2=head1.get('next').get(0).set('data',9);
console.log(head2.get('data'));//9
console.log(head2.get('next').get(0));//未定义
//预期输出:1和{…对象…}==未定义
试试:


const head2=Immutable.setIn(head1,['next',0,'data'],9)
set
按预期工作,它返回一个新对象,其
数据值更新为
9
。但是您从未对
head1
做过任何事情,您只访问其中的值,而不构建新的更新的
head1