@6pm/tuple 中文文档教程

发布于 9年前 浏览 28 项目主页 更新于 3年前

6pm Tuple

构建状态Coverage Status

@6pm/tuple 包将元组实现为不可变的值数组,其中 额外的保证,即每个值的组合都由一个 单例元组实例。

因为单例的性质,这些元组适合做identity 比较 (tuple1 === tuple2),并存储在 SetWeakSet 中,作为 MapWeakMap 的键值。

Installing

npm install @6pm/tuple

Running the test suite

@6pm/tuple 包中。

  • browser tests:
npm test

并按照提供的说明

  • cli tests:
npm install -g mocha
mocha
  • ci tests
npm test ci
  • coverage test
npm run cover

Usage

通过以下任一方式提供值数组来创建元

Tuple.for([ a, b, c ]);

var tuple = Tuple.any([ 1, null, Infinity ]);

Tuple.any([ 1, null, Infinity ]) === tuple; // true

组 他们的成员被默默地忽略了。

var tuple = Tuple.any([ 1, 2, 3 ]);
tuple[1] = -1;

tuple[1]; // 2

for or any?

这两种方法的不同之处在于 any() 允许原始值,而 for() 只允许符合条件的值作为 Map 键,加上 null 和 <代码>未定义。 然而,原始值是有代价的,所以应该与 警告!

这两种方法共享相同的元组空间,例如:

var a = {}, b = {};

Tuple.for([ a, b ]) === Tuple.any([ a, b ]); // true

any

这种分裂的原因是目前没有通用的机制来 允许对 JavaScript 中的值进行弱引用(仅通过键,通过 WeakMap), 所以使用 any() 是有代价的——每个不同的原始值在一个 元组将使用垃圾收集器无法回收的内存。

这意味着 any() 应该谨慎使用,通常是当 可能提供的原语是有界的并且相对较小,否则这 打开内存泄漏的可能性。

for

使用 for() 不会泄漏内存,如果结果的任何元素 元组有资格进行垃圾收集,然后元组本身变成 无法访问,一旦没有直接访问,它最终也会被垃圾收集 存在参考。

6pm Tuple

Build StatusCoverage Status

The @6pm/tuple package implements tuples as immutable arrays of values, with the additional guarantee that each combination of values is represented by a singleton tuple instance.

Because of the singleton nature, these tuples are suitable for identity comparison (tuple1 === tuple2), and storage in Set and WeakSet, and as key values for Map and WeakMap.

Installing

npm install @6pm/tuple

Running the test suite

From within the @6pm/tuple package.

  • browser tests:
npm test

and follow the instructions provided

  • cli tests:
npm install -g mocha
mocha
  • ci tests
npm test ci
  • coverage test
npm run cover

Usage

Create a tuple by supplying an array of values via either:

Tuple.for([ a, b, c ]);

or:

var tuple = Tuple.any([ 1, null, Infinity ]);

Tuple.any([ 1, null, Infinity ]) === tuple; // true

Tuples are rendered immutable, via Object.freeze, so any attempt to modify their members is silently ignored.

var tuple = Tuple.any([ 1, 2, 3 ]);
tuple[1] = -1;

tuple[1]; // 2

for or any?

The two methods differ, in that any() allows primitive values, whereas for() only allows values eligible as Map keys, with the addition of null and undefined. Primitive values come with a cost, though, so should be used with caution!

Both methods share the same tuple space, for example:

var a = {}, b = {};

Tuple.for([ a, b ]) === Tuple.any([ a, b ]); // true

any

The reason for this schism is that there is currently no generic mechanism to allow a weak reference to a value within JavaScript (only by key, via WeakMap), so usage of any() comes with a cost - every distinct primitive value used in a tuple will use memory, that cannot be reclaimed by the garbage collector.

This means that any() should be used cautiously, typically when the set of primitives that may be supplied is bounded and relatively small, otherwise this opens the potential for memory leakage.

for

Using for() does not leak memory, if any of the elements of the resultant tuple become eligible for garbage collection, then the tuple itself becomes unreachable, and it too will eventually be garbage collected, once no direct references exist.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文