TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Immutability in JavaScript: A Contrarian View

4 点作者 sdesalas大约 5 年前

3 条评论

testbot123大约 5 年前
Immutability is used in JavaScript and especially React because it&#x27;s a core computer science concept with real-world benefits, not because it&#x27;s &quot;sexy&quot; or a fad.<p>Arguably the largest benefit in React-land is being able to pass around objects and do equality checks via reference. If I mutate an object and pass it as a prop down the tree, the consumer components won&#x27;t know that it&#x27;s been updated because React does shallow comparison by default (===). Meaning that wherever you have props in your code where you mutate an object, you have to write a function to determine equality, which leads to your code becoming bloated and unreadable, not to mention the massive loss in performance.<p>Mutability also harms our ability to test for expected outputs from given inputs. Mutations can cause side-effects that cause other mutations which can be difficult to see or predict. This means code cannot be accurately tested, and it becomes much more difficult to find bugs and near impossible to get a macro-level view of your application logic. In this sense, mutability can make your application &#x2F;more&#x2F; complex and difficult to maintain and extend.<p>Edit: Please read the other responses in that SO thread. They contain correct information while most of yours is misinformation&#x2F;speculation with an authoritative tone.
评论 #23246990 未加载
sdesalas大约 5 年前
Sure dude. Have you ever used something other than React?
评论 #23248247 未加载
评论 #23248277 未加载
评论 #23248260 未加载
sdesalas大约 5 年前
Feel free to pick a bone.