集合构造器的 .of 和 .from 方法
小于 1 分钟
集合构造器的 .of
和 .from
方法
模块
esnext.set.of
esnext.set.from
esnext.map.of
esnext.map.from
esnext.weak-set.of
esnext.weak-set.from
esnext.weak-map.of
esnext.weak-map.from
类型
interface SetConstructor {
of<T>(...args: Array<T>): Set<T>;
from<T, U>(
iterable: Iterable<T>,
mapFn?: (value: T, index: number) => U,
thisArg?: any
): Set<U>;
}
interface MapConstructor {
of<K, V>(...args: Array<[K, B]>): Map<K, V>;
from<K, V, T>(
iterable: Iterable<T>,
mapFn?: (value: T, index: number) => [key: K, value: V],
thisArg?: any
): Map<K, V>;
}
interface WeakSetConstructor {
of<T extends object>(...args: Array<T>): WeakSet<T>;
from<T, U extends object>(
iterable: Iterable<T>,
mapFn?: (value: T, index: number) => U,
thisArg?: any
): WeakSet<U>;
}
interface WeakMapConstructor {
of<K extends object, V>(...args: Array<[K, V]>): WeakMap<K, V>;
from<K extends object, V, T>(
iterable: Iterable<T>,
mapFn?: (value: T, index: number) => [key: K, value: V],
thisArg?: any
): WeakMap<K, V>;
}
入口点
core-js/proposals/collection-methods
core-js/proposals/collection-of-from
core-js(-pure)/full/set/from
core-js(-pure)/full/set/of
core-js(-pure)/full/map/from
core-js(-pure)/full/map/of
core-js(-pure)/full/weak-set/of
core-js(-pure)/full/weak-set/from
core-js(-pure)/full/weak-map/of
core-js(-pure)/full/weak-map/from
示例
示例:
Set.of(1, 2, 3, 2, 1); // => Set {1, 2, 3}
Map.from(
[
[1, 2],
[3, 4],
],
([key, value]) => [key ** 2, value ** 2]
); // => Map { 1: 4, 9: 16 }