zoukankan      html  css  js  c++  java
  • 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]])
    

      

  • 相关阅读:
    洛谷 P1084 [NOIP2012 提高组] 疫情控制(二分,倍增,贪心)
    洛谷 P2680 [NOIP2015 提高组] 运输计划(二分,树上查分,树上倍增,LCA)
    洛谷 P6858 深海少女与胖头鱼(期望dp)
    洛谷 P2197 【模板】nim 游戏(博弈论)
    洛谷 P1297 [国家集训队]单选错位(期望)
    洛谷 P1288 取数游戏II(博弈论)
    洛谷 P4316 绿豆蛙的归宿(期望)
    P1495 【模板】中国剩余定理(CRT)/曹冲养猪
    【模板】快速乘
    [数论学习笔记]费马小定理、欧拉函数、欧拉定理、欧拉降幂公式
  • 原文地址:https://www.cnblogs.com/xiao-yaolx/p/13218501.html
Copyright © 2011-2022 走看看