Created
March 21, 2019 14:34
-
-
Save ghostcode/68c8b07b71b95fd0b3824ea7cad92314 to your computer and use it in GitHub Desktop.
Vue源码cached函数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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