斐波那契数列 计算对应位置的数值
const result = function(){
let temp = {} //缓存数据,防止重复计算
return function(n){
if(temp[n]){
return temp[n]
}else{
return temp[n] = (n==0 || n==1) ? n : result(n-1) + result(n-2)
}
}()
执行相同的方法,每次执行的间隔时间为 a,a+b,a+2b···a+n*b,n为执行次数
function handle(fn,a,b){
this.a=b
this.b=b
this.time=0 //等同于n
this.timer=null
this.start=()=>{
this.timer=setTimeout(()=>{
fn()
this.time++
this.start()
},this.a+this.time*this.b)
}
this.stop=()=>{
this.timer&&clearTimeout(this.timer)
this.time=0
}
}
实现call功能
Function.prototype.myCall=function(context){
if(!(this instanceof Function)){ //谁调用mycall,this便指向谁
throw new Error('not a function')
}
let obj=context || window //不传入新this时默认为window
let args=[...arguments].slice(1) //arguments为传入的所有内容,拿到除this外的其他参数
if(!(obj instanceof Object)){
// 传入的第一个参数非Object时,防止obj.fn报错
obj={}
}
obj.fn=this //定义obj.fn 等于 this
let result=obj.fn(...args) //将参数传入,执行方法
delete obj.fn //删除obj上添加的fn,避免污染context
return result
}
持续记录中