javscript中自定義鍵值對(Dictionary)

//自定義鍵值對對象
function Dictionary() {
    this.data = new Array();

    this.put = function (key, value) {
        this.data[key] = value;
    };

    this.get = function (key) {
        return this.data[key];
    };

    this.remove = function (key) {
        this.data[key] = null;
    };

    this.isEmpty = function () {
        return this.data.length == 0;
    };

    this.size = function () {
        return this.data.length;
    };
}<pre name="code" class="javascript">//然後就可以通過
var typeArr = new Dictionary();
typeArr.put(1, "1"); //存
typeArr.get(1);//=1 取
typeArr.remove(1);//刪除指定元素
typeArr.isEmpty();//刪除所有元素
typeArr.size();//獲取長度


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章