面向切面编程AOP
Function.prototype.before = function( beforefn ){
let fn = this;
return function(){
beforefn.apply( fn , arguments );
return fn.apply( fn , arguments );
}
};
Function.prototype.after = function( afterfn ){
let fn = this;
return function(){
let result = fn.apply( fn , arguments );
afterfn.apply( fn , arguments );
return result;
}
};
let func = function(name){
console.log( 2,name );
};
func = func.before(function(arg){
console.log( 1,arg );
}).after(function(arg){
console.log( 3,arg );
});
func('aop');
函数柯里化
let currying = function (fn) {
let args = [];
return function () {
if (arguments.length === 0) {
return fn.apply(this, args);
} else {
args.push(...arguments);
return arguments.callee;
}
}
};
let costFn = (function () {
let money = 0;
return function () {
for (let i = 0, l = arguments.length; i < l; i++) {
money += arguments[i];
}
return money;
}
})();
let cost = currying(costFn);
cost(100);
cost(200);
cost(300);
console.log(cost());
function sum(...params){
const cache=this._cache || (this._cache=[])
if(params.length){
this._cache=[...cache,...params]
return this
}else{
return cache.reduce((total,item)=>total+item,0)
}
}
sum(1,2)
sum(3,4)
console.log(sum())
去柯里化
Function.prototype.uncurrying = function () {
let self = this;
return function () {
return Function.prototype.call.apply(self, arguments);
}
};
let push = Array.prototype.push.uncurrying();
let obj = {
"length": 1,
"0": 1
};
push(obj, 2);
console.log(obj);
偏函数
function partial(fn,...argFix){
return function(...argCurr){
let arg=[],idx=0;
for(let i=0;i<argFix.length;i++){
if(argFix[i]===undefined){
arg[i]=argCurr[idx++];
}else{
arg[i]=argFix[i];
}
}
arg=arg.concat(argCurr.slice(idx));
return fn(...arg);
}
}
function add(a,b,c,d,e){
return a*b+c*d+e;
}
let partial_add=partial(add,1,undefined,2,undefined);
console.log(partial_add(3,4,5));
函数节流
let throttle = function (fn, interval) {
let timer,
firstTime = true;
return function () {
let args = arguments,
__self = this;
if (firstTime) {
fn.apply(__self, args);
return firstTime = false;
}
if (timer) {
return false;
}
timer = setTimeout(function () {
clearTimeout(timer);
timer = null;
fn.apply(__self, args);
}, interval || 500);
};
};
window.onresize = throttle(function () {
console.log(1);
}, 500);
函数防抖
function debounce(fn, delay) {
let timer;
return function () {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments)
}, delay || 500)
}
}
box.onmousemove = debounce(function (e) {
box.innerHTML = `${e.clientX}, ${e.clientY}`
}, 1000);
分时函数
let timeChunk = function (arr, fn, count, interval) {
let timer;
let start = function () {
for (let i = 0; i < Math.min(count || 20, arr.length); i++) {
let obj = arr.shift();
fn(obj);
}
};
return function () {
timer = setInterval(function () {
if (arr.length === 0) {
return clearInterval(timer);
}
start();
}, interval || 200);
};
};
let arr = [];
for (let i = 1; i <= 1000; i++) {
arr.push(i);
}
let renderFriendList = timeChunk(arr, function (n) {
let div = document.createElement('div');
div.innerHTML = n;
document.body.appendChild(div);
}, 8);
renderFriendList();
惰性加载
let addEvent = function( elem, type, handler ){
if ( window.addEventListener ){
addEvent = function( elem, type, handler ){
elem.addEventListener( type, handler, false );
}
}else{
addEvent = function( elem, type, handler ){
elem.attachEvent( 'on' + type, handler );
}
}
addEvent( elem, type, handler );
};