Immutable.js 使用教程

Immutable.js 使用教程

immutable-jsImmutable persistent data collections for Javascript which increase efficiency and simplicity.项目地址:https://gitcode.com/gh_mirrors/im/immutable-js

1. 项目的目录结构及介绍

Immutable.js 的 GitHub 仓库结构如下:

immutable-js/
├── dist/
├── docs/
├── src/
├── test/
├── .gitignore
├── .npmignore
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── package.json
└── yarn.lock
  • dist/: 包含编译后的 JavaScript 文件。
  • docs/: 包含项目的文档文件。
  • src/: 包含项目的源代码。
  • test/: 包含项目的测试代码。
  • .gitignore: Git 忽略文件配置。
  • .npmignore: npm 忽略文件配置。
  • .travis.yml: Travis CI 配置文件。
  • CONTRIBUTING.md: 贡献指南。
  • LICENSE: 项目许可证。
  • README.md: 项目说明文档。
  • package.json: 项目依赖和脚本配置。
  • yarn.lock: Yarn 锁定文件。

2. 项目的启动文件介绍

Immutable.js 没有传统意义上的“启动文件”,因为它是一个库,主要通过 npm 或 yarn 安装后在其他项目中使用。你可以通过以下方式引入 Immutable.js:

const { Map } = require('immutable');
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);
console.log(map1.get('b'), map2.get('b')); // 2 50

3. 项目的配置文件介绍

Immutable.js 的配置文件主要是 package.json,其中包含了项目的依赖、脚本和其他元数据。以下是 package.json 的部分内容:

{
  "name": "immutable",
  "version": "4.0.0-rc.12",
  "description": "Immutable persistent data collections for Javascript which increase efficiency and simplicity.",
  "main": "dist/immutable.js",
  "module": "dist/immutable.es.js",
  "unpkg": "dist/immutable.min.js",
  "jsdelivr": "dist/immutable.min.js",
  "typings": "dist/immutable.d.ts",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/immutable-js/immutable-js.git"
  },
  "keywords": [
    "immutable",
    "persistent",
    "lazy",
    "data",
    "datastructure",
    "collection",
    "functional",
    "structure",
    "stateless",
    "sequence",
    "iteration"
  ],
  "author": "Lee Byron",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/immutable-js/immutable-js/issues"
  },
  "homepage": "https://github.com/immutable-js/immutable-js#readme",
  "scripts": {
    "build": "rollup -c",
    "test": "jest",
    "lint": "eslint src",
    "prepublishOnly": "yarn run build"
  },
  "dependencies": {},
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "eslint": "^5.0.0",
    "jest": "^23.0.0",
    "rollup": "^0.60.0",
    "rollup-plugin-babel": "^4.0.0",
    "rollup-plugin-node-resolve": "^3.0.0"
  }
}
  • name: 项目名称。
  • version: 项目版本。
  • description: 项目描述。
  • main: 主入口文件。
  • module: ES 模块入口文件。
  • typings: TypeScript 类型定义文件。
  • scripts: 脚本命令。
  • dependencies: 项目依赖。
  • devDependencies: 开发依赖。

以上是 Immutable.js

immutable-jsImmutable persistent data collections for Javascript which increase efficiency and simplicity.项目地址:https://gitcode.com/gh_mirrors/im/immutable-js

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Immutable.js是一个JavaScript库,用于创建不可变的数据结构。它提供了一组用于创建、操作和查询这些数据结构的API。以下是使用Immutable.js的一些常见步骤: 1. 安装Immutable.js 可以使用npm或yarn安装Immutable.js: ```bash npm install immutable ``` ```bash yarn add immutable ``` 2. 导入Immutable.js 在需要使用Immutable.js的文件中,导入Immutable.js的核心模块: ```js import Immutable from 'immutable'; ``` 3. 创建不可变的数据结构 使用Immutable.js的函数来创建不可变的数据结构,例如: ```js const data = Immutable.fromJS({ foo: { bar: 1 } }); ``` 这将创建一个不可变的Map对象,其中包含一个名为`foo`的键,它的值是包含一个`bar`键和值为1的对象。 4. 操作不可变的数据结构 使用Immutable.js的方法来对不可变数据结构进行操作,例如: ```js const newData = data.setIn(['foo', 'bar'], 2); ``` 这将返回一个新的Immutable对象,其中`foo.bar`的值已经被更新为2。需要注意的是,原始的数据结构并没有被修改,而是返回了一个新的对象来代表更新后的值。 5. 查询不可变的数据结构 使用Immutable.js的方法来查询不可变数据结构,例如: ```js const value = data.getIn(['foo', 'bar']); ``` 这将返回`foo.bar`的值,即1。 以上就是使用Immutable.js的基本步骤。需要注意的是,由于Immutable.js创建的数据结构是不可变的,因此在对其进行操作时需要使用Immutable.js提供的方法,而不是原生的JavaScript方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仰北帅Bobbie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值