手写源代码(一)——set&map

一、Set是ES6提供给我们的构造函数,能够造出一种新的存储数据的结构,只有属性值,成员值唯一(不重复)。

class MySet{
    constructor(iterator){
        //判断是否是可迭代对象
        if(typeof iterator[Symbol.iterator] !== "function"){
            throw new Error(`你提供的${iterator}不是一个课迭代的对象`);
        }
        this._datas = [];
        //循环可迭代对象,将结果加入到set中
        for (const item of iterator) {
            this.add(item);
        }

    }
    add(data){
        if(!this.has(data)){
            this._datas.push(data);
        }
    }
    has(data){
        for (const item of this._datas) {
            if(this.isEqual(data,item)){
                return true;
            }
        }
        return false;
    }
    delete(data){
        for (let i = 0; i < this._datas.length; i++) {
            const element = this._datas[i];
            if(this.isEqual(element,data)){
                this._datas.splice(i,1);
                return true;
            }
            
        }
        return false;
    }
    //遍历
    *[Symbol.iterator](){
        for (const item of this._datas) {
            yield item;
        }
    }
    forEach(callback){
        for (const item of this._datas) {
            callback(item,ietm,this);
        }
    }
    clear(){
        this._datas.length = 0;
    }

    /**
     * 判断两个数据是否相等
     * @param {*} data1 
     * @param {*} data2 
     */
    isEqual(data1,data2){
        if(data1 === 0 && data2 === 0){
            return true;
        }
        return Object.is(data1,data2);
    }
}

二、Map 是ES6提供给我们的构造函数,能够造出一种新的存储数据的结构。本质上是键值对的集合。key对应value,key和value唯一,任何值都可以当属性。

class MyMap{
    constructor(iterable = []){
         //判断是否是可迭代对象
         if(typeof iterable[Symbol.iterator] !== "function"){
            throw new Error(`你提供的${iterable}不是一个课迭代的对象`);
        }
        this._datas = [];
        for (const item of iterable) {
            //item也是一个可迭代的对象
            if(typeof item[Symbol.iterator] !== "function"){
                throw new Error(`你提供的${item}不是一个课迭代的对象`)
            }
            const iterator = item[Symbol.iterator]();
            const key = iterator.next().value;
            const value = iterator.next().value;
            this.set(key,value);
        }

    }
    set(key,value){
        //看里面有没有,如果有,则直接修改key对应的value值
        const obj = this._getObj(key);
        if(obj){
            //修改
            obj.value = value;
        }else{
            this._datas.push({
                key,
                value
            })
        }

    }
    get(key){
        const item = this._getObj(key);
        if(item){
            return item.value;
        }
        return undefined;
    }
    get size(){
        return this._datas.length;
    }
    delete(key){
       for (let i = 0; i < this._datas.length; i++) {
           const element = this._datas[i];
           if(this.isEqual(element.key,key)){
               this._datas.splice(i,1);
               return true;
           }
       }
       return false;
    }
    clear(){
        this._datas.length = 0;
    }
    has(key){
        const item = this._getObj(key);
        return item !== undefined;
    }
    /**
     * 根据key值找到对应的数组项
     * @param {*} key 
     */
    _getObj(key){
        for (const item of this._datas) {
            if(this.isEqual(item.key,key)){
                return item;
            }
        }
        return undefined;
    }
    isEqual(data1,data2){
        if(data1 === 0 && data2 === 0){
            return true;
        }
        return Object.is(data1,data2);
    }
    *[Symbol.iterator](){
        for (const item of this._datas) {
            yield[item.key,item.value];
        }
    }
    forEach(callback){
        for (const item of this._datas) {
            callback(item.value,item.key,this);
        }
    }
}
发布了9 篇原创文章 · 获赞 7 · 访问量 753

猜你喜欢

转载自blog.csdn.net/qq_40619263/article/details/104380888
今日推荐