# Immutable collections for JavaScript
[![Build Status](https://github.com/immutable-js/immutable-js/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/immutable-js/immutable-js/actions/workflows/ci.yml?query=branch%3Amain) [Chat on slack](https://immutable-js.slack.com)
[Read the docs](https://immutable-js.com) and eat your vegetables.
Docs are automatically generated from [README.md][] and [immutable.d.ts][].
Please contribute! Also, don't miss the [wiki][] which contains articles on
additional specific topics. Can't find something? Open an [issue][].
**Table of contents:**
- [Introduction](#introduction)
- [Getting started](#getting-started)
- [The case for Immutability](#the-case-for-immutability)
- [JavaScript-first API](#javascript-first-api)
- [Nested Structures](#nested-structures)
- [Equality treats Collections as Values](#equality-treats-collections-as-values)
- [Batching Mutations](#batching-mutations)
- [Lazy Seq](#lazy-seq)
- [Additional Tools and Resources](#additional-tools-and-resources)
- [Contributing](#contributing)
## Introduction
[Immutable][] data cannot be changed once created, leading to much simpler
application development, no defensive copying, and enabling advanced memoization
and change detection techniques with simple logic. [Persistent][] data presents
a mutative API which does not update the data in-place, but instead always
yields new updated data.
Immutable.js provides many Persistent Immutable data structures including:
`List`, `Stack`, `Map`, `OrderedMap`, `Set`, `OrderedSet` and `Record`.
These data structures are highly efficient on modern JavaScript VMs by using
structural sharing via [hash maps tries][] and [vector tries][] as popularized
by Clojure and Scala, minimizing the need to copy or cache data.
Immutable.js also provides a lazy `Seq`, allowing efficient
chaining of collection methods like `map` and `filter` without creating
intermediate representations. Create some `Seq` with `Range` and `Repeat`.
Want to hear more? Watch the presentation about Immutable.js:
[![Immutable Data and React](website/public/Immutable-Data-and-React-YouTube.png)](https://youtu.be/I7IdS-PbEgI)
[README.md]: https://github.com/immutable-js/immutable-js/blob/main/README.md
[immutable.d.ts]: https://github.com/immutable-js/immutable-js/blob/main/type-definitions/immutable.d.ts
[wiki]: https://github.com/immutable-js/immutable-js/wiki
[issue]: https://github.com/immutable-js/immutable-js/issues
[Persistent]: https://en.wikipedia.org/wiki/Persistent_data_structure
[Immutable]: https://en.wikipedia.org/wiki/Immutable_object
[hash maps tries]: https://en.wikipedia.org/wiki/Hash_array_mapped_trie
[vector tries]: https://hypirion.com/musings/understanding-persistent-vector-pt-1
## Getting started
Install `immutable` using npm.
```shell
# using npm
npm install immutable
# using Yarn
yarn add immutable
# using pnpm
pnpm add immutable
# using Bun
bun add immutable
```
Then require it into any module.
<!-- runkit:activate -->
```js
const { Map } = require('immutable');
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);
map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50
```
### Browser
Immutable.js has no dependencies, which makes it predictable to include in a Browser.
It's highly recommended to use a module bundler like [webpack](https://webpack.github.io/),
[rollup](https://rollupjs.org/), or
[browserify](https://browserify.org/). The `immutable` npm module works
without any additional consideration. All examples throughout the documentation
will assume use of this kind of tool.
Alternatively, Immutable.js may be directly included as a script tag. Download
or link to a CDN such as [CDNJS](https://cdnjs.com/libraries/immutable)
or [jsDelivr](https://www.jsdelivr.com/package/npm/immutable).
Use a script tag to directly add `Immutable` to the global scope:
```html
<script src="immutable.min.js"></script>
<script>
var map1 = Immutable.Map({ a: 1, b: 2, c: 3 });
var map2 = map1.set('b', 50);
map1.get('b'); // 2
map2.get('b'); // 50
</script>
```
Or use an AMD-style loader (such as [RequireJS](https://requirejs.org/)):
```js
require(['./immutable.min.js'], function (Immutable) {
var map1 = Immutable.Map({ a: 1, b: 2, c: 3 });
var map2 = map1.set('b', 50);
map1.get('b'); // 2
map2.get('b'); // 50
});
```
### Flow & TypeScript
Use these Immutable collections and sequences as you would use native
collections in your [Flowtype](https://flowtype.org/) or [TypeScript](https://typescriptlang.org) programs while still taking
advantage of type generics, error detection, and auto-complete in your IDE.
Installing `immutable` via npm brings with it type definitions for Flow (v0.55.0 or higher)
and TypeScript (v2.1.0 or higher), so you shouldn't need to do anything at all!
#### Using TypeScript with Immutable.js v4
Immutable.js type definitions embrace ES2015. While Immutable.js itself supports
legacy browsers and environments, its type definitions require TypeScript's 2015
lib. Include either `"target": "es2015"` or `"lib": "es2015"` in your
`tsconfig.json`, or provide `--target es2015` or `--lib es2015` to the
`tsc` command.
<!-- runkit:activate -->
```js
const { Map } = require('immutable');
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);
map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50
```
#### Using TypeScript with Immutable.js v3 and earlier:
Previous versions of Immutable.js include a reference file which you can include
via relative path to the type definitions at the top of your file.
```js
///<reference path='./node_modules/immutable/dist/immutable.d.ts'/>
import Immutable from 'immutable';
var map1: Immutable.Map<string, number>;
map1 = Immutable.Map({ a: 1, b: 2, c: 3 });
var map2 = map1.set('b', 50);
map1.get('b'); // 2
map2.get('b'); // 50
```
## The case for Immutability
Much of what makes application development difficult is tracking mutation and
maintaining state. Developing with immutable data encourages you to think
differently about how data flows through your application.
Subscribing to data events throughout your application creates a huge overhead of
book-keeping which can hurt performance, sometimes dramatically, and creates
opportunities for areas of your application to get out of sync with each other
due to easy to make programmer error. Since immutable data never changes,
subscribing to changes throughout the model is a dead-end and new data can only
ever be passed from above.
This model of data flow aligns well with the architecture of [React][]
and especially well with an application designed using the ideas of [Flux][].
When data is passed from above rather than being subscribed to, and you're only
interested in doing work when something has changed, you can use equality.
Immutable collections should be treated as _values_ rather than _objects_. While
objects represent some thing which could change over time, a value represents
the state of that thing at a particular instance of time. This principle is most
important to understanding the appropriate use of immutable data. In order to
treat Immutable.js collections as values, it's important to use the
`Immutable.is()` function or `.equals()` method to determine _value equality_
instead of the `===` operator which determines object _reference identity_.
<!-- runkit:activate -->
```js
const { Map } = require('immutable');
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = Map({ a: 1, b: 2, c: 3 });
map1.equals(map2); // true
map1 === map2; // false
```
Note: As a performance optimization Immutable.js attempts to return the existing
collection when an operation would result in an identical collection, allowing
for using `===` reference equality to determine if something definitely has not
changed. This can be extremely useful when used within a memoization function
which would prefer to re-run the function if a deeper equality check could
potentially
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
eclipse-php-2023-12-R-linux-gtk-x86-64.tar.gz (2000个子文件)
e4-dark_globalstyle.css 9KB
e4-dark_ide_colorextensions.css 9KB
e4-light_ide_colorextensions.css 5KB
e4-dark_win.css 5KB
sample.css 5KB
narrow_book.css 4KB
e4-dark_tabstyle.css 4KB
e4-dark_partstyle.css 4KB
book.css 4KB
e4-dark_preferencestyle.css 3KB
e4-light_tabstyle.css 3KB
e4_default_mac.css 3KB
e4-dark_mac1013.css 3KB
e4_default_gtk.css 3KB
e4_default_win.css 2KB
e4-light_globalstyle.css 2KB
e4-dark_mac.css 2KB
e4_classic.css 2KB
e4-dark_linux.css 2KB
whatsnew.css 1KB
e4_basestyle.css 1KB
high-contrast.css 875B
e4-light_partstyle.css 860B
e4-light-drag-styling.css 831B
e4-dark-drag-styling.css 769B
e4_globalstyle.css 761B
disabled_book.css 356B
macosx_narrow_book.css 269B
migrate.css 188B
overview.css 177B
jvmti.h 80KB
jni.h 74KB
classfile_constants.h 22KB
jawt.h 12KB
jdwpTransport.h 8KB
jvmticmlr.h 5KB
jni_md.h 2KB
jawt_md.h 2KB
tomcat_charset.html 18KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-v20.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-v20.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-2.0.html 17KB
epl-v20.html 17KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
YunFeiDong
- 粉丝: 150
- 资源: 3867
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功