test

//1.手动实现localStorage
if (!window.localStorage) {
    //首先定义window中的 属性
    Object.defineProperty(window, "localStorage", new (function () {
        var aKeys = [], oStorage = {};
        Object.defineProperty(oStorage, "getItem", {
            value: function (sKey) { return sKey ? this[sKey] : null; },
            writable: false,
            configurable: false,
            enumerable: false
        });
        Object.defineProperty(oStorage, "key", {
            value: function (nKeyId) { return aKeys[nKeyId]; },
            writable: false,
            configurable: false,
            enumerable: false
        });
        // 利用cookie  进行保存  并设置过期时间
        Object.defineProperty(oStorage, "setItem", {
            value: function (sKey, sValue) {
                if (!sKey) { return; }
                document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
            },
            writable: false,
            configurable: false,
            enumerable: false
        });
        Object.defineProperty(oStorage, "length", {
            get: function () { return aKeys.length; },
            configurable: false,
            enumerable: false
        });
        Object.defineProperty(oStorage, "removeItem", {
            value: function (sKey) {
                if (!sKey) { return; }
                document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
            },
            writable: false,
            configurable: false,
            enumerable: false
        });
        this.get = function () {
            var iThisIndx;
            for (var sKey in oStorage) {
                iThisIndx = aKeys.indexOf(sKey);
                if (iThisIndx === -1) { oStorage.setItem(sKey, oStorage[sKey]); }
                else { aKeys.splice(iThisIndx, 1); }
                delete oStorage[sKey];
            }
            for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) { oStorage.removeItem(aKeys[0]); }
            for (var aCouple, iKey, nIdx = 0, aCouples = document.cookie.split(/\s*;\s*/); nIdx < aCouples.length; nIdx++) {
                aCouple = aCouples[nIdx].split(/\s*=\s*/);
                if (aCouple.length > 1) {
                    oStorage[iKey = unescape(aCouple[0])] = unescape(aCouple[1]);
                    aKeys.push(iKey);
                }
            }
            return oStorage;
        };
        this.configurable = false;
        this.enumerable = true;
    })());
}

//2.Node Events 模块
class EventEmitter {
    constructor() {
        //事件监听函数保存的地方
        this.events = {};
    }
    on(eventName, listener) {
        if (this.events[eventName]) {
            this.events[eventName].push(listener);
        } else {
            //如果没有保存过,将回调函数保存为数组
            this.events[eventName] = [listener];
        }
    }
    emit(eventName) {
        //emit触发事件,把回调函数拉出来执行
        this.events[eventName] && this.events[eventName].forEach(listener => listener())
    }
}
let event = new EventEmitter();
event.on('嗨', function () {
    console.log('你好');
});
event.emit('嗨');


//3.区间合并 比较区间的第二个元素 如果后一个大于前一个 则进行合并  否则不合并
var merge = function (attrs) {
    if (attrs.length < 2) return attrs
    attrs.sort((a, b) => a[0] - b[0])  //先进行排序
    let curr = attrs[0]  //存储数组内的一个集合
    let result = []
    for (let attr of attrs) {
        if (curr[1] >= attr[0]) {
            curr[1] = Math.max(curr[1], attr[1])
        } else {
            result.push(curr)
            curr = attr
        }
    }
    if (curr.length !== 0) {
        result.push(curr)
    }
    return result
};
merge([[1, 3], [2, 6], [8, 10], [15, 18]])

  

posted @ 2020-07-01 12:11  逍丶遥  阅读(101)  评论(0编辑  收藏  举报