js模拟hashtable的简单实例_javascript技巧

复制代码 代码如下:

function Hashtable()//自定义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]; } }
}

复制代码 代码如下:

// js哈希表
function HashTable() {

    this.ObjArr = {};

    this.Count = 0;

    //添加
    this.Add = function(key, value) {
        if (this.ObjArr.hasOwnProperty(key)) {
            return false; //如果键已经存在,不添加
        }
        else {
            this.ObjArr[key] = value;
            this.Count++;
            return true;
        }
    }

    //是否包含某项
    this.Contains = function(key) {
        return this.ObjArr.hasOwnProperty(key);
    }

    //取某一项 其实等价于this.ObjArr[key]
    this.GetValue = function(key) {
        if (this.Contains(key)) {
            return this.ObjArr[key];
        }
        else {
            throw Error("Hashtable not cotains the key: " + String(key)); //脚本错误
            //return;
        }
    }

    //移除
    this.Remove = function(key) {
        if (this.Contains(key)) {
            delete this.ObjArr[key];
            this.Count--;
        }
    }

    //清空
    this.Clear = function() {
        this.ObjArr = {}; this.Count = 0;
    }
}

测试代码:
//员工
function employee(id, userName) {
    this.id = id;
    this.userName = userName;
}

function test() {

    var ht = new HashTable();
    var tmpEmployee = null;
    for (var i = 1; i < 6; i++) {
        tmpEmployee = new employee(i, "Employee_" + i);
        ht.Add(i, tmpEmployee);
    }
    for (var i = 1; i <= ht.Count; i++) {
        alert(ht.GetValue(i).userName); //其实等价于ht.ObjArr[i].userName
        //alert(ht.ObjArr[i].userName);
    }
    ht.Remove(1);
    alert(ht.Contains(1)); //false
    alert(ht.Contains(2)); //true
    //alert(ht.GetValue(1)); //异常
    var result = ht.GetValue(2);
    if (result != null) {
        alert("Employee Id:" + result.id + ";UserName:" + result.userName);
    }
    ht.Add(2, "这一个key已经存在!"); //Add无效
    //ht.Clear(); //清空
    alert(ht.Count);

}

时间: 2024-12-23 03:17:03

js模拟hashtable的简单实例_javascript技巧的相关文章

js模拟hashtable的简单实例介绍

 本篇文章主要是对js模拟hashtable的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助  代码如下: function Hashtable()//自定义hashtable {     this._hash = new Object();     this.add = function(key, value) {         if (typeof (key) != "undefined") {             if (this.contains(key

js格式化时间的简单实例_javascript技巧

Date.prototype.format = function(format) { //author: meizz let o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds

JS 拼凑字符串的简单实例_javascript技巧

和Java一样,JS中直接用"+"号拼凑字符串是很耗费资源的,所以在大量拼凑字符串的情景中,我们也需要一个类似于StringBuffer的工具, 下面利用Array.join()方法实现StringBuffer function StringBuffer() { this._strs = new Array; } StringBuffer.prototype.append = function (str) { this._strs.push(str); }; StringBuffer.

js实现StringBuffer的简单实例_javascript技巧

实现: function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.Append = function (str) { this.__strings__.push(str); return this; }; //格式化字符串 StringBuffer.prototype.AppendFormat = function (str) { for (var i = 1; i < arguments.length;

js复制内容到剪贴板代码,js复制代码的简单实例_javascript技巧

如下所示: <script type="text/JavaScript"> function jsCopy(){ var e=document.getElementById("contents");//对象是contents e.select(); //选择对象 document.execCommand("Copy"); //执行浏览器复制命令 } </script> <textarea id="conte

js实现楼层效果的简单实例_javascript技巧

今天自己写个楼层效果,有一点烦躁,小地方犯错误.各位大神来修改不足啊!!! <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>楼层切换</title> <style> *{padding: 0;margin: 0;} li{list-style: none;} .main img{width:

JS获取checkbox的个数简单实例_javascript技巧

JS获取多选框checkbox被选中的个数. var checkbox = document.getElementsByName("likes[]"); //此处通过此种方式才能获得多选框为数组. //like为name = "like[]" , 获得时必须加上[] var checked_counts = 0; for(var i=0;i<checkbox.length;i++){ if(checkbox[i].checked){ //被选中的checkbo

js方法数据验证的简单实例_javascript技巧

实例如下: //input标签只能正数字 <input onkeyup="this.value=this.value.replace(/[^1-9]/g,'')" > //只能输入正整数 function CheckNum(thisobj) { if (thisobj.value == "0") { } else { var firstNum = thisobj.value.toString().substring(0, 1); if (thisobj.

修改js confirm alert 提示框文字的简单实例_javascript技巧

修改js confirm alert 提示框文字的简单实例 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <style> *{padding: 0; margin: 0;} html{ height: 100%; } body{ font-size: 16px; font-family: "Microsoft Yahe