How does this JavaScript function cache its results?

After reading this several times, I still do not understand how this sample code from page 76 Stoyan Stefanov "JavaScript Templates" works. I'm not a ninja yet. But for me, it reads as if it were storing an empty object:

var myFunc = function (param) {
  if (!myFunc.cache[param]) {
    var result = {};
    // ... expensive operation ...
    myFunc.cache[param] = result;
  } 
  return myFunc.cache[param];
};
// cache storage
myFunc.cache = {};

If this invisible “expensive operation” does not return to result, I see nothing.

Where are the results stored?

PS: I read Caching Result Function Returns from John Resig Learning Advanced JavaScript , which is a similar exercise, and I get it. But here the code is different.

+3
source share
3 answers

- , result.

, .

+5

, . :

myFunc , . "param", . , , ( ), .

+2

- , , var var ( )

+1
source

All Articles