Javascript中的字典类
这是MyMap类的完整实现-
示例
class MyMap {
constructor() {
this.container = {};
}
display() {
console.log(this.container);
}
hasKey(key) {
return key in this.container;
}
put(key, value) {
this.container[key] = value;
}
delete(key) {
if (this.hasKey(key)) {
delete this.container[key];
return true;
}
return false;
}
get(key) {
return this.hasKey(key) ? this.container[key] : undefined;
}
keys() {
return Object.keys(this.container);
}
values() {
let values = []; for (let key in this.container) {
values.push(this.container[key]);
}
return values;
}
clear() {
this.container = {};
}
forEach(callback) {
for (let prop in this.container) {
//调用回调为:callback(key,value)
callback(prop, this.container[prop]);
}
}
}热门推荐
8 初八的祝福语简短
10 新年发给客户祝福语简短
11 中班入学祝福语简短大全
12 高考祝福语 简短12字
13 18岁的祝福语简短
14 答谢老师的简短祝福语
15 出院祝福语简短8字
16 婚礼亲友祝福语简短幽默
17 薛之谦祝福语简短
18 送给同桌的祝福语简短