为什么不可变性在 JavaScript 中如此重要(或需要)? - Why is immutability so important (or needed) in JavaScript?

问题:

I am currently working on React JS and React Native frameworks.我目前正在研究React JSReact Native框架。 On the half way road I came across Immutability or the Immutable-JS library , when I was reading about Facebook's Flux and Redux implementation.在半路上,当我阅读 Facebook 的 Flux 和 Redux 实现时,我遇到了 Immutability 或Immutable-JS 库

The question is, why is immutability so important?问题是,为什么不变性如此重要? What is wrong in mutating objects?改变对象有什么问题? Doesn't it make things simple?这不是让事情变得简单吗?

Giving an example, let us consider a simple News reader app with the opening screen being a list view of news headlines.举个例子,让我们考虑一个简单的新闻阅读器应用程序,它的打开屏幕是新闻标题的列表视图。

If I set say an array of objects with a value initially I can't manipulate it.如果我设置了一个初始的对象数组,我将无法操作它。 That's what immutability principle says, right?这就是不变性原则所说的,对吧? (Correct me if I am wrong.) But, what if I have a new News object that has to be updated? (如果我错了,请纠正我。)但是,如果我有一个新的 News 对象需要更新怎么办? In usual case, I could have just added the object to the array.通常情况下,我可以将对象添加到数组中。 How do I achieve in this case?在这种情况下我如何实现? Delete the store and recreate it?删除商店并重新创建它? Isn't adding an object to the array a less expensive operation?将一个对象添加到数组中不是一个成本较低的操作吗?


解决方案:

参考一: https://en.stackoom.com/question/2KHAh
参考二: https://stackoom.com/question/2KHAh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章