Joined December 2012
·
Posted to
jQuery caching
over 1 year
ago
Simple but smart. I love it.
I particularly like that you don't have the overhead of pre-caching your selectors in separate variables at the top of your method, namespace, or constructor. Have you tried this in a project with Backbone? I typically cache my elements as properties of my View instances: myNavView.$nav = $('#nav'). That could simply become $$('#nav'), no?
Achievements
124 Karma
3,959 Total ProTip Views
Walrus
The walrus is no stranger to variety. Use at least 4 different languages throughout all your repos
Mongoose
Have at least one original repo where Ruby is the dominant language
Nephila Komaci
Have at least one original repos where PHP is the dominant language
Charity
Fork and commit to someone's open source project in need
True, but is there a reason why you wouldn't make count a property of the object? Or maybe the title of this pro-tip should be "Use closures to protect local variables from methods in the prototype chain"? Kidding.
For the sake of your example, it would be nice to see you instantiate counter() as myCount and call myCount.decrement() to show where the undefined error will occur in the order of execution. Then, changing var counter to this.counter would fix the rest. Or, conversely, show how your get() method could be used in decrement().