Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

精读《Records & Tuples 提案》 #384

Closed
ascoders opened this issue Dec 25, 2021 · 2 comments
Closed

精读《Records & Tuples 提案》 #384

ascoders opened this issue Dec 25, 2021 · 2 comments

Comments

@ascoders
Copy link
Owner

ascoders commented Dec 25, 2021

也许你觉得 js 通过 immutablejs、immer 等库已经很好的支持了 immutable,但看下面这个问题:

你在控制台看到应用数据从 { a: 1 } 变成了 { a: 1},能解读出什么?这两个对象是否相同完全取决于引用是否相同,但引用是否相同我们根本看不出来,这取决于如何实现的。也就是说,{ a: 1} === { a: 1 } 或者 { a: 1} !== { a: 1 } 都有可能。

对,就是这个问题,在现在 js 是无解的。你可以保证 “我实现严格遵循 immutable”,但我反问 “鬼知道你的实现有没有问题??”,稍微不小心写个默认值就会导致一个新引用 obj ?? [],谁敢保证不犯错呢?

https://github.com/tc39/proposal-record-tuple 解决的就是这个问题,它让肉眼看上去的相等绝对相等: #{ a: 1 } === #{ a: 1},原因是通过 # 符号填补了之前隐晦的引用关系。


精读《Records & Tuples 提案》

@ascoders ascoders changed the title 精读《record-tuple 提案》 精读《Records & Tuples 提案》 Dec 25, 2021
@LastKnightCoder
Copy link

发现文中有一个矛盾的地方

所以 Object.keys(#{a: 1, b:2}) === #["a", "b"]

@ascoders
Copy link
Owner Author

发现文中有一个矛盾的地方

所以 Object.keys(#{a: 1, b:2}) === #["a", "b"]

看上去是提案写错了,第二段代码是错误的,因为 Object.keys(#{ a: 1, b: 2 }) 结果是一个普通数组。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants