js重载代码

const searcher={}

addMethod(searcher,'find',()=>{

console.log('查询所有')

})

addMethod(searcher,'find',(name)=>{

console.log('按姓名查询')

})

addMethod(searcher,'find',(first,last)=>{

console.log('按姓与名查询')

})

function addMethod(object,name,fn){

const old=object[name]

object[name]=function(...args){

if(args.length===fn.length){

return fn.apply(this,args)

}else if(typeof old ==='function'){

return old.apply(this,args)

}

}

}

searcher.find('name')

你可能感兴趣的:(前端)