javaScript-Hashtable
这是我同事有JavaScript模拟的HashTable
我有的地方看不明白,麻烦讲解下,谢谢
function Hashtable()
{
this._hash = new Object();
this.add = function(key,value){
if(typeof(key)!= "undefined "){
if(this.contains(key)==false){
this._hash[key]=typeof(value)== "undefined "?null:value;
return true;
} else {
return false;
}
} else {
return false;
}
}
this.remove = function(key){delete this._hash[key];}
this.count = function(){var i=0;for(var k in this._hash){i++;} return i;}
this.items = function(key){return this._hash[key];}
this.contains = function(key){ return typeof(this._hash[key])!= "undefined ";}
this.clear = function(){for(var k in this._hash){delete this._hash[k];}}
}
我想知道this._hash[key]是什么意思,
那里来的这个东西?
[解决办法]
自定义属性 最上面不就定义了吗
Object
[解决办法]
this._hash[key]
Javascript语法问题,this._hash对象里名为key的属性或者方法,和this._hash.key意思差不多,但使用更灵活
[解决办法]
key就是一个参数啊
js里的对象是这样的:
var _hash = { key: value, key: value ,...........}
也就是一个个的name value 对.有两种引用方式
_hash.propertyname or _hash[propertyname]
前一种引用方式容易出问题,所以用后边的更保险一些.