Like your $$ approach, I created a function (with the same name) that uses a remembering pattern to store a global cleaner and also takes into account the second context parameter ... for example $$ (". Class", "#context"). This is necessary if you use the find () chain that appears after $$ is returned; therefore, it will not be cached alone unless you first cache the context object. I also added a boolean parameter at the end (2nd or 3rd parameter depending on the context used) to make it return to the DOM.
the code:
function $$(a, b, c){ var key; if(c){ key = a + "," + b; if(!this.hasOwnProperty(key) || c){ this[key] = $(a, b); } } else if(b){ if(typeof b == "boolean"){ key = a; if(!this.hasOwnProperty(key) || b){ this[key] = $(a); } } else{ key = a + "," + b; this[key] = $(a, b); } } else{ key = a; if(!this.hasOwnProperty(key)){ this[key] = $(a); } } return this[key]; }
Using:
<div class="test">a</div> <div id="container"> <div class="test">b</div> </div>โ <script> $$(".test").append("1"); </script>
Sefi Grossman Jul 20 '12 at 17:10 2012-07-20 17:10
source share