How is this JavaScript function caching its results?

心已入冬 提交于 2019-12-04 10:07:40

You've answered your own question -- the author assumes that the expensive operation will store its result in result.

The cache would otherwise only contain empty objects, as you've noted.

the results are being stored in the object literal called 'cache'. What the code is specifically doing is:

when myFunc gets executed with a param, the function first checks the cache. If there is a value for 'param' in the cache, it returns it. If not, you do the expensive operation, and then cache the result(with param as the key), so the next time the function is called with the same param the cache is used.

It says // expensive operation - the inference is that you implement code there which assigns variables into the result var, or sets the result var to another Object (which is the result of an expensive operation)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!