Caching the return results of a function from John Resig's Learning Advanced JavaScript -


I have some questions about the exercise # 1 of John Reciig about this ceremony

  1. what is the purpose of the second last line getElements.cache = {}; Does it accumulate the return result in an array?

  2. If my guess (1) is correct, is it just holding return result , because the code in the "f" section of the function > GetElements.cache [name] = results; ?

  3. When I play with the code in the tutorial in the console, I get the result from the line GetElements.cache [name] = "to" section , but Still the same result was achieved, as it was there - that is, he told me that there were 76 elements, therefore, what is the purpose of this line getElements.cache [name] = result Not necessary for?

  4. There is no significance for the fact that, in the "F" section of the function, line getElements.cache [name] = the sequence of results Is replaced by , if section, where it says result = getElements.cache [name]

  5. Finally, a pre-defined function in cache javascript? I can not find it in the documentation function getElements (name) {var results; If (getElements.cache [name]) {Results = getElements.cache [name]; } And {result = document.getElementsByTagName (name); GetElements.cache [name] = Results; } Return results; } GetElements.cache = {}; Log ("found element:", getElements ("pre"). Length); A) Second from the last line, determines that the property "cache" on the object is " GetElements is an object originally to start that property.

    b) It will be a cache, think of getElements as an object and the cache is a hash which is catching on the results.

    c) Yes, you'll still get the same result because the cache is just caching, this is not changing anyway, it will only speed things up potentially.

    d) Yes, the cache is caching a result, removing the result of the second cache.

    e) No This is a custom property that was defined for "getElements", any number of these can be defined with different names.

Comments