代码只实现最基本的功能, 没有添加容错机制
function myMap() {
this.bucketLength = 8;
this.init();
}
// 使用hash, 桶
// 初始化
myMap.prototype.init = function () {
// bucket的长度
this.bucket = new Array(this.bucketLength);
// 遍历
for (let i = 0; i < this.bucket.length; i++) {
this.bucket[i] = {
type: 'bucket_' + i,
next: null
}
}
}
// 创建一个hash 桶来获取对应的值所对应的typeof
// 1 (0, 8] 规定hash的值在0-8之间
// 2, 如果传入的key的typeof 相同, return hash值不变
myMap.prototype.makeHash = function (key) {
let hash = 0;
// string
if (typeof key !== 'string') {
if (typeof key == 'number') {
// number NaN
hash = Object.is(key, NaN) ? 0 : key;
} else if (typeof key == 'object') {
// [] {}
hash = 1;
} else if (typeof key == 'boolean') {
hash = new Number(key);
} else {
// function
hash = 2;
}
} else {
// 通过获取字符串的前几位,转换为ASCII 求模,让所的值在0-8之间
for (let i = 0; i < 6; i++) {
hash += key[i] ? key[i].charCodeAt(0) : 0;
}
}
return hash % 8;
}
// set
myMap.prototype.set = function (key, value) {
// 1 makeHas 传key
let hash = this.makeHash(key);
// 2 获取到key所对应的类型的 bucket
let tempHash = this.bucket[hash];
// 当tempHash.next有,才继续下一个next,以此类推
while (tempHash.next) {
// 判断传入的key是否存在
if (tempHash.next.key == key) {
tempHash.next.value = value;
// 防止死循环
return ;
} else {
// 不存在就直接添加给下一个next(桶)
tempHash = tempHash.next;
}
}
// next 为 null 直接进行赋值
tempHash.next = {
key: key,
value: value,
next: null
}
return tempHash;
}
// set
myMap.prototype.get = function (key) {
let hash = this.makeHash(key);
let tempHash = this.bucket[hash];
while(tempHash.next){
if(tempHash.next.key == key){
return true;
}
}
return false;
}
// delete
myMap.prototype.delete = function(key){
let hash = this.makeHash(key);
let tempHash = this.bucket[hash];
// while(tempHash.next){
if(tempHash.next.key == key){
tempHash.next == tempHash.next.next;
return true
}
// }
return false;
}
myMap.prototype.has = function (key) {
let hash = this.makeHash(key);
let tempHash = this.bucket[hash];
if(tempHash.next.key == key){
return true;
}
return false;
}
myMap.prototype.clear = function () {
this.bucket = new Array(0);
}
let oMap = new myMap();
// console.log(oMap.set('name', false));
console.log(oMap.set('name', false))
console.log(oMap.set([], 1))
oMap.set({}, 'object')
// console.log(oMap.get('name'));
// console.log(oMap.get('name1'));
// console.log(oMap.get({}));
// console.log(oMap.delete([]));//false 引用值存在地址, [] 虽然相同但是地址不同
// console.log(oMap.delete('name'));