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]);
}
}
}热门推荐
10 天气转凉祝福语简短
11 同事离别祝福语简短的
12 团队文案祝福语简短励志
13 关于开车的祝福语简短
14 英语简短祝福语给学生
15 发小结婚简短祝福语
16 给孙子拜年祝福语简短
17 舅妈生日红包祝福语简短
18 给侄子祝福语简短精辟