Skip to content

Instantly share code, notes, and snippets.

@ghostcode
Created March 21, 2019 14:34
Show Gist options
  • Select an option

  • Save ghostcode/68c8b07b71b95fd0b3824ea7cad92314 to your computer and use it in GitHub Desktop.

Select an option

Save ghostcode/68c8b07b71b95fd0b3824ea7cad92314 to your computer and use it in GitHub Desktop.
Vue源码cached函数
function cached(callback){
let cached = {}
return function(id){
const hit = cached[id]
return hit || (cached[id] = callback[id])
}
}
//使用
const idToTemplate = cached(id => {
const el = query(id)
return el && el.innerHTML
})
//这样多次调用如下:idToTemplate(template),不会多次执行query方法
template = idToTemplate(template)
template = idToTemplate(template)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment