clone

中文文档
2.1.2发布于 2018/03/22MITCommonJS零依赖近 30 天下载 3.5亿

deep cloning of objects and arrays

原始使用文档
文档目录8 节
  1. Installation
  2. Example
  3. API
  4. Circular References
  5. Test
  6. Caveat
  7. Bugs and Issues
  8. License

clone

build status

info badge

offers foolproof deep cloning of objects, arrays, numbers, strings etc. in JavaScript.

Installation

npm install clone

(It also works with browserify, ender or standalone.)

Example

var clone = require('clone');

var a, b;

a = { foo: { bar: 'baz' } };  // initial value of a

b = clone(a);                 // clone a -> b
a.foo.bar = 'foo';            // change a

console.log(a);               // show a
console.log(b);               // show b

This will print:

{ foo: { bar: 'foo' } }
{ foo: { bar: 'baz' } }

clone masters cloning simple objects (even with custom prototype), arrays, Date objects, and RegExp objects. Everything is cloned recursively, so that you can clone dates in arrays in objects, for example.

API

clone(val, circular, depth)

  • val -- the value that you want to clone, any type allowed

  • circular -- boolean

    Call clone with circular set to false if you are certain that obj contains no circular references. This will give better performance if needed. There is no error if undefined or null is passed as obj.

  • depth -- depth to which the object is to be cloned (optional, defaults to infinity)

clone.clonePrototype(obj)

  • obj -- the object that you want to clone

Does a prototype clone as described by Oran Looney.

Circular References

var a, b;

a = { hello: 'world' };

a.myself = a;
b = clone(a);

console.log(b);

This will print:

{ hello: "world", myself: [Circular] }

So, b.myself points to b, not a. Neat!

Test

npm test

Caveat

Some special objects like a socket or process.stdout/stderr are known to not be cloneable. If you find other objects that cannot be cloned, please open an issue.

Bugs and Issues

If you encounter any bugs or issues, feel free to open an issue at github or send me an email to [email protected]. I also always like to hear from you, if you’re using my code.

License

Copyright © 2011-2015 Paul Vorbach and contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

版本与发布记录

clone 共发布 38 个版本,首次发布于 2011/09/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.42018/03/228 年前
2.1.2latest2018/03/228 年前
1.0.32017/11/098 年前
2.1.12017/03/109 年前
2.1.02016/11/239 年前
2.0.02016/09/2910 年前

关于 clone 的常见问题

clone 是什么?

deep cloning of objects and arrays

如何安装 clone?

在项目目录运行 npm install clone,或使用 pnpm add / yarn add / bun add。

clone 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/clone 是否存在。

clone 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

clone 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

clone 可以商用吗?

clone 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

clone 的下载量有多少?

根据 npm 官方下载统计,clone 在 2026/10/05 – 2026/11/03 这 30 天内被下载 350,886,745 次,日均约 11,696,225 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

clone 还在维护吗?

最新版本 2.1.2 发布于 2018/03/22(8 年前);至今共发布 38 个版本,其中过去 12 个月发布了 0 个;首次发布于 2011/09/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。