增加Map的一个增加值的方法
This commit is contained in:
parent
9394e0def8
commit
b39bb08048
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user