博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

仿HashTable的Js

Posted on 2011-04-26 21:24  Adam哥  阅读(411)  评论(0编辑  收藏  举报
 <script type="text/javascript">
        function HashTable() {
            this._hashtable = {};
            if (typeof (_hashtable_initialized) == "undefined") {
                HashTable.prototype.Add = function(key, value) {
                    if (key in this._hashtable) {
                        return false;
                    }
                    this._hashtable[key] = value;
                    return true;
                }
                HashTable.prototype.Remove = function(key) {
                    return delete (this._hashtable[key]);
                }
                HashTable.prototype.Count = function() {
                    var i = 0;
                    for (var k in this._hashtable)
                    { i++; }
                    return i;
                }
                HashTable.prototype.Items = function(key) {
                    return this._hashtable[key];
                }
                HashTable.prototype.Clear = function() {
                    for (var k in this._hashtable)
                    { delete this._hashtable[k]; }
                    return true;
                }
                HashTable.prototype.Contains = function(key) {
                    return typeof (this._hashtable[key]) != "undefined";
                }
                _hashtable_initialized = true;
            }
        }
    </script>

counter