Caching Function Return Results from John Resig Learning Advanced JavaScript

I have a few questions about this feature from John Rezig exercise # 19 at http://ejohn.org/apps/learn/#19

  • What is the purpose of the second last row getElements.cache = {};Does it return resultssave in an array?

  • If my assumption in (1) is correct, then this only catches return results, because in the "else" section of the function getElements.cache[name] = results;?

  • when I played with the code in the console in the tutorial, I deleted the line getElements.cache[name] = resultsfrom the "else" section, but still got the same result as when it was there, namely: it told me that there were 76 elements. So what is the purpose of this line getElements.cache[name] = resultsif there is no need to get the result?

  • is there any value for getElements.cache[name] = results;changing the order of the line in the "else" section of the function from the ifcode section where it saysresults = getElements.cache[name]

  • Finally, is there a cachepredefined function in JavaScript? I can not find it in the documentation.

function getElements( name ) { 
  var results; 

  if ( getElements.cache[name] ) { 
    results = getElements.cache[name]; 
  } else { 
    results = document.getElementsByTagName(name); 
    getElements.cache[name] = results; 
  } 

  return results; 
} 
getElements.cache = {}; 

log( "Elements found: ", getElements("pre").length );
+3
source share
3 answers

a) The second in the last line sets that the "cache" property on the getElements object is an object. Essentially initializing this property.

b) This will be a cache, think of getElements as an object, and the cache is a hash that holds onto the results.

c) Yes, you still get the same result, because the cache just caches, it does not change the answer anyway, it just potentially speeds up the process.

d) , , .

e) , , "getElements", .

+4

1.- line getElements.cache = {}; ?

2.- (1) , , , "else" , getElements.cache[name] = results;?

, , DOM.

3.- , getElements.cache[name] = results "else" , , , , , 76 . , getElements.cache[name] = results ?

, , , DOM.

4.- - "else" , getElements.cache[name] = results; if , results = getElements.cache[name]

else, -, results , . results .

5.- , - JavaScript? .

. ( JavaScript .

:

function getElements( name ) { 
  var results; 

  if (getElements.cache[name]) { 
    results = getElements.cache[name]; // Use the cached value
  } else { 
    results = document.getElementsByTagName(name); // Get the desired value
    getElements.cache[name] = results; // cache the result
  } 

  return results; 
} 

getElements.cache = {}; // Initialize the cache
+2

// Were going to get some things.
var getSomeThings = function() {
    // Calling something expense to get the things.
    var things = somethingExpensive();
    // overwriting this function with a function that returns things
    getSomeThings = function() {
         return things;
    }
    // call the new function that returns things and return things.
    return getSomeThings();
};

, . , ,... , .

, . , , .

[ Edit ]

What this means is creating a variable with a name getSomeThings. Then it assigns a function to it. When you call a function, it has access to it getSomeThingsas a variable and can set it using a new function.

For instance:

function getSomethingExpensive() {
     ...
     return 42;
}
var b = function() {
     b = getSomethingExpensive();
}    
b();
alert(b);
+2
source

All Articles