ES6简单map结构实现
class myMap{
constructor(obj){
this.obj = {}
this.length = 0
if(Array.isArray(obj)){
obj.every((item, index, arr) => {
if(Array.isArray(item)){
this.obj[item[0]] = item[1]
this.length += 1
return true
}else{
this.obj = {}
this.length = 0
return false
}
})
}
}
has(key){
return this.obj.hasOwnProperty(key)
}
size(){
return this.length
}
get(key){
if(this.has(key)){
return this.obj[key]
}
}
set(key, value){
if(this.has(key)){
this.obj[key] = value
}else{
this.obj[key] = value
this.length += 1
}
return this
}
keys(){
return Object.keys(this.obj)
}
values(){
return Object.values(this.obj)
}
entries(){
return Object.entries(this.obj)
}
delete(key){
if(this.has(key)){
this.length -= 1
delete this.obj[key]
}
return this
}
clear(){
this.obj = {}
this.length = 0
return this
}
}
let my_Map = new myMap()
my_Map.set('foo', true).set('foo2', false).set('foo2', 1)
console.log(my_Map);
let map = new Map()
map.set('foo', true)
map.set('foo2', false)
console.log(map);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。