码迷,mamicode.com
首页 > Web开发 > 详细

js自定义Map

时间:2014-07-22 08:37:36      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:io   for   cti   re   c   ar   

function Map() {
    this.elements = new Array();
 //得到map的大小
    this.size = function() {
        return this.elements.length;
    }
 //判断是否为空
    this.isEmpty = function() {
        return (this.elements.length < 1);
    }
    //清空
    this.clear = function() {
        this.elements = new Array();
    }
    //放进map一个对象
    this.put = function(_key, _value) {
        this.elements.push( {
            key : _key,
            value : _value
        });
    }
 //根据键去除一个对象
    this.remove = function(_key) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key && typeof this.elements[i].key == typeof _key) {
                    this.elements.splice(i, 1);
                    return true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    }
 //根据键得到一个对象
    this.get = function(_key) {
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key && typeof this.elements[i].key == typeof _key) {
                    return this.elements[i].value;
                }
            }
        } catch (e) {
            return null;
        }
    }
 //返回指定索引的一个对象
    this.element = function(_index) {
        if (_index < 0 || _index >= this.elements.length) {
            return null;
        }
        return this.elements[_index];
    }
 //是否包含键
    this.containsKey = function(_key) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].key == _key && typeof this.elements[i].key == typeof _key) {
                    bln = true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    }
 //是否包含值
    this.containsValue = function(_value) {
        var bln = false;
        try {
            for (i = 0; i < this.elements.length; i++) {
                if (this.elements[i].value == _value && typeof this.elements[i].value == typeof _value) {
                    bln = true;
                }
            }
        } catch (e) {
            bln = false;
        }
        return bln;
    }
 //得到所有的值
    this.values = function() {
        var arr = new Array();
        for (i = 0; i < this.elements.length; i++) {
            arr.push(this.elements[i].value);
        }
        return arr;
    }
 //得到所有的键
    this.keys = function() {
        var arr = new Array();
        for (i = 0; i < this.elements.length; i++) {
            arr.push(this.elements[i].key);
        }
        return arr;
    }
}

js自定义Map,布布扣,bubuko.com

js自定义Map

标签:io   for   cti   re   c   ar   

原文地址:http://my.oschina.net/u/727394/blog/287934

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!