JavaScript patterns (dot com)

I had this idea of collecting better javascript practices and ... well, patterns. The thing is that there is so much bad javascript floating around from the time of "copy/paste this to make your status bar go crazy", so there should be more of the good JS kind to replace the bad one. I liked the idea so much, I even bought the domain jspatterns.com. Until recently I haven't done much, but now there's a list of patterns I wanted to talk about and also some of them already have articles. Check it out.

Today I just added "Load-time branching" and "Singleton2", enjoy responsibly :) And yeah, it's a Wiki, I'm still debating on the best way to approach this. The Wiki is open to anyone, so if you have something to add/change - be my guest.

This entry was posted on Sunday, July 22nd, 2007 and is filed under JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

Somewhat related posts

5 Responses to “JavaScript patterns (dot com)”

  1. David Foley Says:

    Had a look at jspatterns.com - glad to see someone providing a resource for applied javascript patterns! Had a quick glance through the site (I know its early days) and read over the Singleton discussions. Object literals as implementations of the Singleton pattern are fine, but there’s something I’d like to bring to your attention.

    In Singleton2, the instance property lives on the constructor, and as such is externally modifiable which breaks encapsulation. If you’re interested in more detail, email me, but for the moment, you might want to consider the following:

    
    /**
     * toSingleton, transforms javascript classes into singletons
     * @copyright David Foley 2007
     * free as in beer!
     */
    
    function MyClass ()
    {
    	var _value = null;
    	this.setValue = function (value) {
    		_value = value;
    	};
    	this.getValue = function () {
    		return _value;
    	};
    };
    
    /**
     *	@param {Function} Class, the class to transform into a singleton
     *@return {Object} an object with one method, getInstance, which returns an instance of Class
     */
    
    function toSingleton (Class)
    {
    	// return the results of an anonymours function
    	return (function(){
    		// variable instance exists in this closure
    		// private and cannot be modified externally
    		var  instance = null;
    		// the following object literal is returned
    		// as the final result of the toSingleton transformation
    		// it can access the variable 'instance', but
    		// nothing else can, in keeping with priniciple
    		// of encapsulation
    		return {
    			getInstance: function()
    			{
    				if( instance == null) instance = new Class();
    				return instance;
    			}
    		};
    	})();
    };
    
    // overwrite MyClass with the result of the toSingleton transformation
    MyClass = toSingleton(MyClass);
    
    // show it works
    MySingletonReference = MyClass.getInstance();
    MyClass.getInstance().setValue("1");
    alert(MySingletonReference.getValue());
    MySingletonReference.setValue("2");
    alert(MyClass.getInstance().getValue());
    alert(MySingletonReference.getValue() == MyClass.getInstance().getValue());
    
  2. Stoyan Says:

    This is interesting you suggest. I liked my example because you get to keep the new Single() syntax and the class takes care of returning the same object.

    But it’s true that keeping the reference as a property of the constructor function makes it available for modification.

    I’ll play around you your idea of keeping the reference private but still be able to just say:
    new Sungle(), new Single() and they both point to the same object.

  3. Stoyan Says:

    Ha! I figured out that I can use a method that keeps the instance secure. Check out Single2.1 ;)
    http://www.jspatterns.com/index.php?title=Singleton2#Singleton_2.1

  4. Kunal Cholera Says:

    Hey Stoyan Stefanov,

    I had a chance to interview with you for summer internship at Santa Monica Office.

    You were talking about writing a book on object oriented javascript !! Whens it coming out ? I have a keen interest in JavaScript..

  5. Stoyan Says:

    Hey Kunal,

    I remember you, of course.

    The book is not published yet, but interestingly enough, it was announced just today on the publisher’s website:
    http://www.packtpub.com/object-oriented-javascript-applications-libraries/book

    Best regards!

Leave a Reply