爲什麼不可變性在 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章