lodash func
lodash func
by 郭 凡
HTML
<script src="//cdn.bootcss.com/lodash.js/4.6.1/lodash.js"></script>
JavaScript
//lodash函数种种
//bind
//使用bind显示绑定this
function say(){
return 'Say ' + this.what;
}
let sayHello = _.bind(say, {what: 'hello'});//this-->{what : 'helo'}
console.log(sayHello());
//使用bind,从实参中获取
function sayWhat(what){
return 'Say ' + what;
}
let sayHaha = _.bind(sayWhat, {name : 'this'});//
console.log(sayHaha('haha'));
//after
var complete= 0,
collection= _.range(9999999),
progress= _.noop;//return undefined
function work(value){
progress();
}
function reportProgress(){
console.log(++complete + '%');//记录进度
//after的第一个形参表示先执行n-1次reportProgress,花费n-1次相当的时间,到n次的时候正真执行reportProgress
//本方法也实现了递归
progress=complete<100? _.after(0.01*collection, reportProgress) : _.noop;
}
//先把进度启动起来
reportProgress();
//遍历集合的过程就是执行progress方法的过程
//而progress的执行取决于变量complete的值是否小于100
_.forEach(collection, work);