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

js实现hash

时间:2014-05-01 13:44:25      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   javascript   color   string   cti   rgb   type   

由于项目中用到了hash,自己实现了一个。

mamicode.com,码迷
Hash = function () {
    }
    Hash.prototype = {
        constructor: Hash,
        add: function (k, v) {
            if (!this.hasOwnProperty(k)) {
                this[k] = v;
            }
        },
        remove: function (k) {
            if (this.hasOwnProperty(k)) {
                delete this[k];
            }
        },
        update: function (k, v) {
            this[k] = v;
        },
        has: function (k) {
            var type = typeof k;
            if (type === ‘string‘ || type === ‘number‘) {
                return this.hasOwnProperty(k);
            } else if (type === ‘function‘ && this.some(k)) {
                return true;
            }
            return false;
        },
        clear: function () {
            for (var k in this) {
                if (this.hasOwnProperty(k)) {
                    delete this[k];
                }
            }
        },
        empty: function () {
            for (var k in this) {
                if (this.hasOwnProperty(k)) {
                    return false;
                }
            }
            return true;
        },
        each: function (fn) {
            for (var k in this) {
                if (this.hasOwnProperty(k)) {
                    fn.call(this, this[k], k, this);
                }
            }
        },
        map: function (fn) {
            var hash = new Hash;
            for (var k in this) {
                if (this.hasOwnProperty(k)) {
                    hash.add(k, fn.call(this, this[k], k, this));
                }
            }
            return hash;
        },
        filter: function (fn) {
            var hash = new Hash;
            for (var k in this) {

            }
        },
        join: function (split) {
            split = split !== undefined ? split : ‘,‘;
            var rst = [];
            this.each(function (v) {
                rst.push(v);
            });
            return rst.join(split);
        },
        every: function (fn) {
            for (var k in this) {
                if (this.hasOwnProperty(k)) {
                    if (!fn.call(this, this[k], k, this)) {
                        return false;
                    }
                }
            }
            return true;
        },
        some: function (fn) {
            for (var k in this) {
                if (this.hasOwnProperty(k)) {
                    if (fn.call(this, this[k], k, this)) {
                        return true;
                    }
                }
            }
            return false;
        },
        find: function (k) {
            var type = typeof k;
            if (type === ‘string‘ || type === ‘number‘ && this.has(k)) {
                return this[k];
            } else if (type === ‘function‘) {
                for (var _k in this) {
                    if (this.hasOwnProperty(_k) && k.call(this, this[_k], _k, this)) {
                        return this[_k];
                    }
                }
            }
            return null;
        }
    };
mamicode.com,码迷

 

js实现hash,码迷,mamicode.com

js实现hash

标签:style   blog   class   code   java   javascript   color   string   cti   rgb   type   

原文地址:http://www.cnblogs.com/pigtail/p/3700989.html

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