封装方法

1 实现数组的reduce方法

if (!Array.prototype.reduceC) {
    Array.prototype.reduceC = function(callback /*, initialValue*/) {
        'use strict';
        if (this === null) {
            throw new TypeError('Array.prototype.reduceC called on null or undefined');
        }
        if (typeof callback !== 'function') {
            throw new TypeError(callback + ' is not a function');
        }
        var t = Object(this), len = t.length >>> 0, k = 0, value;
        if (arguments.length == 2) {
            value = arguments[1];
        } else {
            while (k < len && !(k in t)) {
                k++; 
            }
            if (k >= len) {
                throw new TypeError('Reduce of empty array with no initial value');
            }
            value = t[k++];
        }
        for (; k < len; k++) {
            if (k in t) {
                value = callback(value, t[k], k, t);
            }
        }
        return value;
    };
}

 2 阻止scroll()事件多次执行

var tur = true;
window.onscroll = function (){
    if(tur){
        setTimeout(scro,500); 
        tur = false; //杀死变量
    }else{

    } 
};
//需要执行的函数
function scro(){
    alert(1);
    tur = true;//复活变量
};

 

posted @ 2020-04-21 10:58  seeBetter  阅读(150)  评论(0)    收藏  举报