JS18 -- 高阶函数

let k = true;

k && fn()

if(k) {
  fn()    
}

  

1、柯里化:https://www.jianshu.com/p/2975c25e4d71

2.1、防抖

<script>

function debounce(fn,delay){
  let timer = null // 借助闭包
  return function() {
    timer && clearTimeout(timer)
    timer = setTimeout(fn,delay)
  }
}


function showTop () {
  let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  console.log('滚动条位置', scrollTop)
}
window.onscroll = debounce(showTop,1000)

</script>

2.2节流

3、惰性函数

// 惰性函数: 当我们需要重复使用一个逻辑的时候,优化逻辑判断,提高 JavaScript 性能
// 原理:同名函数覆盖
// http://www.fly63.com/article/detial/1612
function person(name) {            // 函数1
    console.log(0)
    if(name == "jack") {
        return person = function() {            // 函数2
            console.log("this is jack");
        }
    } else if(name == "tom") {
        return person = function() {            // 函数3
            console.log("this is tom");
        }
    } else {
        return person = function() {            // 函数4
            console.log("this is others");
        }
    }
}
person("jack")();
console.log(1);
person();

 

 4、级联函数

// 级联函数:在每个方法中将对象本身 return 出去
// https://mp.weixin.qq.com/s/gX89lZyfsGCqIfaS8LJ73Q
function Person() {}
// 添加几个方法
Person.prototype = {
    setName: function (name) {
        this.name = name
        return this
    },
    setAge: function (age) {
        this.age = age
        return this
    },
    setSex: function (sex) {
        this.sex = sex
    },
}
Person.constructor = Person    // 别忘了重新指定一下构造函数
let person = new Person()
// 修改前
/*
person.setName('游荡de蝌蚪')
person.setAge(18)
person.setSex('male')
console.log(person)
*/
// 修改之后
person.setName('游荡de蝌蚪').setAge(18).setSex('male')
console.log(person)

 

 

 

 

判断汉字表情😂和英文字母

 function CheckChina(str) { return str.charCodeAt(str) > 255 ? 2 : 1; } 

正则判断汉字

 unction CheckChinese(str) { var reg = new RegExp("[\\u4E00-\\u9FFF]","g"); return reg.test(str) ? 2 : 1; } 

 

https://www.cnblogs.com/xinzaiyuan/p/12684108.html

posted @ 2020-04-16 10:02  Yo!  阅读(161)  评论(0编辑  收藏  举报