利用reduce >

image.png


JS高阶编程技巧: 惰性思想【懒】

image.png

  1. // 基于函数重构实现惰性思想--------------------
  2. let css=function (elem,attr){
  3. if('getComputedStyle' in window){
  4. css=function(elem,attr){
  5. return getComputedStyle(elem)[attr];
  6. }
  7. }else{
  8. css=function(elem,attr){
  9. return elem.currentStyle[attr];
  10. }
  11. }
  12. return css(elem,attr);
  13. }