学习JavaScript高级第八天(闭包高级应用)
1、闭包应用值惰性函数和柯理化函数
function getCss(ele
, attr
){
if(window
.getComputedStyle
) {
return window
.getComputedStyle(ele
)[attr
];
} else {
return ele
.currentStyle
[attr
];
}
}
let isComputed
= 'getComputedStyle' in window
;
function getCss(ele
, attr
){
if(isComputed
) {
return window
.getComputedStyle(ele
)[attr
];
} else {
return ele
.currentStyle
[attr
];
}
}
function getCss(ele
, attr
){
if(window
.getComputedStyle
) {
getCss = function(ele
, attr
) {
return window
.getComputedStyle(ele
)[attr
];
}
} else {
getCss = function(ele
, attr
) {
return ele
.currentStyle
[attr
];
}
}
return getCss(ele
, attr
);
}
console
.log(getCss(document
.body
, 'width'));
console
.log(getCss(document
.body
, 'height'));
console
.log(getCss(document
.body
, 'padding'));
function fn(...arg1
){
return function(...arg2
) {
let arr
= arg1
.concat(arg2
);
return arr
.reduce((total
, item
) => {
return total
+ item
;
});
};
}
let res
= fn(1,2)(3);
console
.log(res
);
function reduce(arr
, callback
, init
) {
arr
= arr
.slice(0);
let index
= 0;
if(typeof init
=== 'undefined') {
init
= arr
[0];
index
=1;
}
for(let i
=index
; i
<arr
.length
; i
++) {
init
= callback(init
, arr
[i
], i
);
}
return init
;
}
let arr
= [10,20,30,40];
let result
= reduce(arr
, function(res
, item
, index
){
console
.log(index
, item
);
return res
+ item
;
});
let result1
= reduce(arr
, function(res
, item
, index
){
console
.log(index
, item
)
return res
+ item
;
}, 100);
let result2
= reduce([90], function(res
, item
, index
){
console
.log(index
, item
)
return res
+ item
;
});
console
.log(result
, result1
, result2
)
2、闭包应用之compose组合函数
function compose(...funcs
){
return function(x
){
if(funcs
.length
< 1) return x
;
funcs
= funcs
.reverse();
console
.log(funcs
);
return funcs
.reduce((res
, item
) => {
console
.log(res
, item
)
return item(res
);
}, x
)
}
}
const add1 = (x
) => x
+1;
const mul3 = (x
) => x
*3;
const div2 = (x
) => x
/2;
let re
= compose(div2
,mul3
,add1
,add1
)(0);
console
.log(re
);
3、闭包应用之函数的防抖和节流
function debounce(fn
, wait
, immediate
=false){
wait
= wait
|| 300;
let timer
= null;
return function(ev
){
clearTimeout(timer
);
console
.log(timer
);
if(immediate
&& (timer
=== null)) {
fn
.call(this, ev
);
}
timer
= setTimeout(()=>{
timer
= null;
if(!immediate
) {
fn
.call(this, ev
)
}
}, wait
);
}
}
function handle(ev
) {
console
.log(this, ev
);
console
.log('防抖');
}
app
.onclick
= debounce(handle
, 500, false); */
function throttle(fn
, wait
= 100) {
let enterTime
= new Date();
return function(){
let backTime
= new Date();
if(backTime
- enterTime
> wait
) {
fn
.apply(this, arguments
);
enterTime
= backTime
;
}
}
}
function throttle(fn
, wait
=300){
let timer
= null,
previous
= 0;
return function(ev
){
let now
= new Date(),
cha
= wait
- (now
- previous
);
if(cha
<= 0 ){
clearTimeout(timer
);
previous
= now
;
fn
.call(this, ev
);
} else if(!timer
){
timer
= setTimeout(() =>{
timer
= null;
previous
= new Date();
fn
.call(this, ev
)
},cha
)
}
}
}
function handle() {
console
.log('节流');
}
window
.onscroll
= throttle(handle
, 500);
转载请注明原文地址: https://lol.8miu.com/read-4835.html