代码展示
- 节流函数
function throttle(fn,time){ var startTime = new Date(); return function(){ var time_ = (new Date() - startTime) >= time; if(time_){ fn.apply(this); startTime = new Date(); } }}function fn(){ console.log("触发了事件");}window.onscroll = throttle(fn,1000);
- 高阶函数、偏函数
function makeUniqueStringFunction(start) { var COUNTER = start; return function(prefix) { return [prefix, COUNTER++].join(''); }};var uniqueString = makeUniqueStringFunction(0);uniqueString("dari");//=> "dari0"uniqueString("dari");//=> "dari1"