Javascript 无法读取属性';长度';hashMap中未定义的
我正在尝试使用hashmap和Javascript 无法读取属性';长度';hashMap中未定义的,javascript,data-structures,hashmap,Javascript,Data Structures,Hashmap,我正在尝试使用hashmap和 Cannot read property 'length' of undefined 这是我的密码 class HashMap{ table = new Array(17); get(key){ let idx = hashfun(key, this.table.length) return this.table[idx] } set(key, value){
Cannot read property 'length' of undefined
这是我的密码
class HashMap{
table = new Array(17);
get(key){
let idx = hashfun(key, this.table.length)
return this.table[idx]
}
set(key, value){
let idx = hashfun(key, this.table.length)
return this.table[idx]
}
}
function hashfun(s, arrlen){
console.log("***",s)
let hash = 17;
for(let i=0; i< s.length; i++){
hash = (13 * hash * s.charCodeAt(i)) % arrlen;
}
return hash
}
let ht = new HashMap();
ht.set("trying")
ht.get()
类HashMap{
表=新阵列(17);
获取(键){
设idx=hashfun(key,this.table.length)
返回此.表[idx]
}
设置(键、值){
设idx=hashfun(key,this.table.length)
返回此.表[idx]
}
}
函数hashfun(s,arrlen){
console.log(“***”,s)
设hash=17;
for(设i=0;i
我正在尝试将键转换为Int,并将键分配给索引。如果我做错了什么,请告诉我。您忘记将
键
传递给get()
调用。(您的set()
调用也没有值)。