跳至主要內容

Map.prototype.emplace

小于 1 分钟featurees-proposal

Map.prototype.emplaceopen in new window

模块

类型

interface Map<K, V> {
  emplace<T, U>(
    key: any,
    handler: {
      update: (value: V, key: K, target: Map<K, V>) => T;
      insert: (key: K, target: Map<K, V>) => U;
    }
  ): T | U;
}

interface WeakMap<K extends object, V> {
  emplace<T, U>(
    key: any,
    handler: {
      update: (value: V, key: K, target: WeakMap<K, V>) => T;
      insert: (key: K, target: WeakMap<K, V>) => U;
    }
  ): T | U;
}

入口点

core-js/proposals/map-upsert-stage-2
core-js(-pure)/full/map/emplace
core-js(-pure)/full/weak-map/emplace

示例

示例open in new window:

const map = new Map([["a", 2]]);

map.emplace("a", { update: (it) => it ** 2, insert: () => 3 }); // => 4

map.emplace("b", { update: (it) => it ** 2, insert: () => 3 }); // => 3

console.log(map); // => Map { 'a': 4, 'b': 3 }