es6数据结构map源码封装

267 阅读1分钟
<script>
    // let arr = [1, 2, 3];
    // //特点 有序,不定长,支持栈队列操作
    // //FILO arr.push arr.pop
    // //FIFO arr.push arr.shift

    // //ES6新的数据结构
    // //Map Set
    // //Map 字典,加强的对象
    // let map = new Map();
    // let obj = {};
    // map.set(1, 1);
    // obj["1"] = 1;
    // // 赋值
    // map.set(1, "aa");
    // obj["1"] = "aa";
    // //输出
    // console.log(map.get(1))
    // console.log(obj["1"])

    // console.log(map, obj);

    // //若干键值对的集合
    // console.log(map.has(1));
    // console.log(map.has("1"))
    // console.log(map.has(2))
    //     //删除
    // map.delete(1)
    // console.log(map.has(1))
    // map.clear()

    //hash 算法
    function myMap() {
        this.init();
    }
    myMap.prototype.len = 8;
    myMap.prototype.bucket = [];
    myMap.prototype.init = function() {
        for (let i = 0; i < this.len; i++) {
            this.bucket[i] = {
                next: null
            }
        }
    }
    myMap.prototype.makeHash = function(key) {
        let hash = 0
        if (typeof key == "string") {
            //取后三位字符串
            let len = (key.length > 3) ? key.length : 3;
            for (let i = len - 3; i < len; i++) {
                hash += (key[i] !== undefined ? key[i].charCodeAt() : 0)
            }
        } else {
            hash = +key
        }
        return hash;
    }
    myMap.prototype.set = function(key, value) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.key === key) {
                nextNode.value = value;
                return
            } else {
                nextNode = nextNode.next;
            }
        }
        nextNode.next = {
            key,
            value,
            next: null
        }
    }
    myMap.prototype.get = function(key) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.key === key) {
                return nextNode.value;
            } else {
                nextNode = nextNode.next;
            }
        }
        return
    }
    myMap.prototype.has = function(key) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.key === key) {
                return true;
            } else {
                nextNode = nextNode.next;
            }
        }
        return false;
    }
    myMap.prototype.deleat = function(key) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.next.key === key) {
                nextNode.next = nextNode.next.next;
                return true;
            } else {
                nextNode = nextNode.next;
            }
        }
        return false;
    }
    myMap.prototype.clear = function() {
        this.init();
    }
    let map = new myMap();
</script>