This code fragment will return an element from the cache, if it was previously selected or selected, the cache and returns the element. It is useful for updating the contents of elements that have never been substantially changed (i.e., the parent element of the counter that the user sees where the number changes, but the parent element does not). The code is as follows:
var $$ = (function() { var cache = {}; return (function (selector) { return cache[selector] || ( cache[selector] = jQuery (selector) ); }); })();
You can use it like this:
$$('#id')
Now ... how does it work? How does $$ have access to the jQuery selector? It has nothing to do with $$ , starting with $ , you can also make var foo . How $$ match what is passed into it on selector . I would expect to see var selector = argumentName inside $$ . Also, it seems to me that $$ configured to accept arguments (for example, function (input) {}), but does it easily do?
This small piece of code is incredibly confusing to me, and clarity would be greatly appreciated. Thanks!
source share