- Mastering Immutable.js
- Adam Boduch
- 146字
- 2021-07-08 10:30:07
Passing JavaScript collections
You can pass native JavaScript collections, such as objects and arrays, to Immutable.js collections, such as maps and lists. This is one way to provide immutable collections with initial values. For example, you can pass the list constructor an array, as follows:
const myList = List([1, 2, 3]);
console.log('myList', myList.get(1));
// -> myList 2
The Map constructors work the same way, except you pass it an object:
const myMap = Map({ a: 1, b: 2, c: 3 });
console.log('myMap', myMap.get('b'));
// -> myMap 2
Passing array or object literals to Immutable.js collections like this is fine. You should avoid passing in references to arrays or objects, however. The reason is that once you create an Immutable.js collection, the data is supposed to be immutable. If you have a reference to a mutable array or object in your code, this just leads to confusion.
推薦閱讀
- Clojure Programming Cookbook
- Python從小白到大牛
- Ceph Cookbook
- C++面向對象程序設計(微課版)
- 圖解Java數據結構與算法(微課視頻版)
- Java編程指南:基礎知識、類庫應用及案例設計
- RTC程序設計:實時音視頻權威指南
- Designing Hyper-V Solutions
- Learning Laravel 4 Application Development
- Unity&VR游戲美術設計實戰
- Apache Camel Developer's Cookbook
- OpenStack Networking Essentials
- 零基礎學Python編程(少兒趣味版)
- 網絡數據采集技術:Java網絡爬蟲實戰
- Emotional Intelligence for IT Professionals