增加Map的一个增加值的方法

This commit is contained in:
zhl 2021-01-11 10:10:22 +08:00
parent 9394e0def8
commit b39bb08048

View File

@ -861,3 +861,25 @@ Object.defineProperties(Array.prototype, {
}
});
interface Map<K, V> {
/**
* V为number的Map, , V, set
* V为其他类型时, set
* @param key
* @param value
*/
inc?(key: K, value: V): this;
}
Object.defineProperties(Map.prototype, {
inc: {
value: function<K, V> (key: K, value: V) {
if (typeof value == 'number') {
this.set(key, (this.get(key) || 0) + value );
} else {
this.set(key, value);
}
}
}
});