Link Dump: JavaScript Singletons
Posted in Development
A couple of links to posts and articles demonstrating the Singleton pattern in JavaScript:
- Objectifying JavaScript by Jonathan Snook
- The singleton pattern by Simon Willison (discussion at Simon’s blog)
- Design Patterns in JavaScript: Singleton
- Getting Funky With Scopes and Closures by Mark Wubben
I noticed people create JS Singletons either via new function(){...} or function (){...}(); Douglas Crockford makes an interesting point about it at the Yahoo! UI blog:
By using
newto invoke the function, the object holds onto a worthlessprototypeobject. That wastes memory with no offsetting advantage. If we do not use the new, we don’t keep the wasted prototype object in the chain. So instead we will invoke the factory function the right way, using().
I feel like I’m putting lipstick on a pig, because this is anything but the canonical Singleton as we know it.
