<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to make a singleton in javascript</title>
	<atom:link href="http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: josh09</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-850</link>
		<dc:creator>josh09</dc:creator>
		<pubDate>Sun, 29 Nov 2009 03:04:09 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-850</guid>
		<description>great lesson, I needed it</description>
		<content:encoded><![CDATA[<p>great lesson, I needed it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: How to write a singleton class in javascript &#124; Javascript Kata</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-796</link>
		<dc:creator>How to write a singleton class in javascript &#124; Javascript Kata</dc:creator>
		<pubDate>Wed, 30 Sep 2009 12:59:57 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-796</guid>
		<description>[...] I look at my stats, a lot of people are wondering how to write a singleton class. I already wrote about it before but my old solution exposed the instance of the class so more than one instance could be created [...]</description>
		<content:encoded><![CDATA[<p>[...] I look at my stats, a lot of people are wondering how to write a singleton class. I already wrote about it before but my old solution exposed the instance of the class so more than one instance could be created [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-736</link>
		<dc:creator>Phil</dc:creator>
		<pubDate>Fri, 10 Apr 2009 16:04:57 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-736</guid>
		<description>Hi! I&#039;ve read all your posts and I&#039;m very glad of this discussion. I wonna give something back to all readers and posters in describing a small scenario, where singleton is more than just OOP-Global, and wonna show you, why I think that choosing the right singleton implementation is very important.

I&#039;m deloping a huge project including a JS-Framework. Therefore it&#039;s necessary to inherit classes. So imagine the following example:

We have a class Container and a inherited class FullFadeWindow. The Container-Class represents a Container-Widget that can be instantiated many times and that can contain data loaded per AJAX. The FullFadeWindow is a special window: its always on top and its covering the full screen (or browser window). So I want FullFadeWindow to be a singleton class, inheriting from Container, that is no singleton class.

This can&#039;t be done using a pure static implementation (like Dan&#039;s Log-Example), because of the prototype inheritance. Also the closures-solutions cannot be used this way, because all functions need to be declared in a local context which make prototype inheritance impossible. And because I cannot guarantee that nobody calls my constructor, if I&#039;m using Dan&#039;s first idea, the remaining possibility is MadKat&#039;s one above. This way, we can simple make an existing class to a singleton one, we can use prototype inheritance, and we do not need to &quot;milk&quot; JS to brutally make this object based language to an complete object oriented one.

In addition, the way described in http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html (posted by Antony above) is also not usable in this scenario.

So, if you plan to use prototype inheritance (you know, like X.prototype = new Y()), you should consider MadKat&#039;s solution.

Have fun,
   Phil</description>
		<content:encoded><![CDATA[<p>Hi! I&#8217;ve read all your posts and I&#8217;m very glad of this discussion. I wonna give something back to all readers and posters in describing a small scenario, where singleton is more than just OOP-Global, and wonna show you, why I think that choosing the right singleton implementation is very important.</p>
<p>I&#8217;m deloping a huge project including a JS-Framework. Therefore it&#8217;s necessary to inherit classes. So imagine the following example:</p>
<p>We have a class Container and a inherited class FullFadeWindow. The Container-Class represents a Container-Widget that can be instantiated many times and that can contain data loaded per AJAX. The FullFadeWindow is a special window: its always on top and its covering the full screen (or browser window). So I want FullFadeWindow to be a singleton class, inheriting from Container, that is no singleton class.</p>
<p>This can&#8217;t be done using a pure static implementation (like Dan&#8217;s Log-Example), because of the prototype inheritance. Also the closures-solutions cannot be used this way, because all functions need to be declared in a local context which make prototype inheritance impossible. And because I cannot guarantee that nobody calls my constructor, if I&#8217;m using Dan&#8217;s first idea, the remaining possibility is MadKat&#8217;s one above. This way, we can simple make an existing class to a singleton one, we can use prototype inheritance, and we do not need to &#8220;milk&#8221; JS to brutally make this object based language to an complete object oriented one.</p>
<p>In addition, the way described in <a href="http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html" rel="nofollow">http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html</a> (posted by Antony above) is also not usable in this scenario.</p>
<p>So, if you plan to use prototype inheritance (you know, like X.prototype = new Y()), you should consider MadKat&#8217;s solution.</p>
<p>Have fun,<br />
   Phil</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Antony S</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-725</link>
		<dc:creator>Antony S</dc:creator>
		<pubDate>Fri, 13 Mar 2009 05:28:04 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-725</guid>
		<description>A very good and simple article on singleton in JS.

http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html</description>
		<content:encoded><![CDATA[<p>A very good and simple article on singleton in JS.</p>
<p><a href="http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html" rel="nofollow">http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-718</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Sun, 22 Feb 2009 18:33:59 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-718</guid>
		<description>@zoips @jb
I really like your alternatives. I&#039;ll write a new article with that. Thanks a lot.</description>
		<content:encoded><![CDATA[<p>@zoips @jb<br />
I really like your alternatives. I&#8217;ll write a new article with that. Thanks a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jb</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-717</link>
		<dc:creator>jb</dc:creator>
		<pubDate>Sun, 22 Feb 2009 17:54:58 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-717</guid>
		<description>well a very simple way of having singleton is this
Test= function(){
      if(!Test.prototype.instance)
          Test.prototype.instance=this;
       else
	  return Test.prototype.instance;
}

^^</description>
		<content:encoded><![CDATA[<p>well a very simple way of having singleton is this<br />
Test= function(){<br />
      if(!Test.prototype.instance)<br />
          Test.prototype.instance=this;<br />
       else<br />
	  return Test.prototype.instance;<br />
}</p>
<p>^^</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zoips</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-709</link>
		<dc:creator>zoips</dc:creator>
		<pubDate>Mon, 09 Feb 2009 20:43:34 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-709</guid>
		<description>You can ensure no one wipes out your singleton reference by wrapping it all up in a closure:

var MySingleton = (function()
{
    var instance = null;

    return function()
    {
        if (instance !== null)
        {
            return instance;
        }

        instance = this;

        //init code
    };
})();

var s1 = new MySingleton();
var s2 = new MySingleton();

s1 === s2; // true</description>
		<content:encoded><![CDATA[<p>You can ensure no one wipes out your singleton reference by wrapping it all up in a closure:</p>
<p>var MySingleton = (function()<br />
{<br />
    var instance = null;</p>
<p>    return function()<br />
    {<br />
        if (instance !== null)<br />
        {<br />
            return instance;<br />
        }</p>
<p>        instance = this;</p>
<p>        //init code<br />
    };<br />
})();</p>
<p>var s1 = new MySingleton();<br />
var s2 = new MySingleton();</p>
<p>s1 === s2; // true</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MadKat</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-696</link>
		<dc:creator>MadKat</dc:creator>
		<pubDate>Mon, 05 Jan 2009 19:33:55 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-696</guid>
		<description>Dan,

I&#039;m so glad that you have this code blog. I wasn&#039;t able to find exactly what I was looking for, but I definitely got pointed in the right direction. Here&#039;s my implementation of a singleton:

function Singleton() {
  if (Singleton.instance == null) {
    Singleton.instance = this;
    Singleton.instance.initialize();
  }
  else {
    // Do not want the extra object
    delete this;
  }

  return Singleton.instance;
}

Singleton.prototype = {
  instance: null,
  initialize: function() {
    // Actual constructor for your one instance
  }
}

This way, I can call new Singleton() and get the same instance, without the need for getInstance().

var instance1 = new Singleton();
var instance2 = new Singleton();

alert(instance1 == instance2); // True</description>
		<content:encoded><![CDATA[<p>Dan,</p>
<p>I&#8217;m so glad that you have this code blog. I wasn&#8217;t able to find exactly what I was looking for, but I definitely got pointed in the right direction. Here&#8217;s my implementation of a singleton:</p>
<p>function Singleton() {<br />
  if (Singleton.instance == null) {<br />
    Singleton.instance = this;<br />
    Singleton.instance.initialize();<br />
  }<br />
  else {<br />
    // Do not want the extra object<br />
    delete this;<br />
  }</p>
<p>  return Singleton.instance;<br />
}</p>
<p>Singleton.prototype = {<br />
  instance: null,<br />
  initialize: function() {<br />
    // Actual constructor for your one instance<br />
  }<br />
}</p>
<p>This way, I can call new Singleton() and get the same instance, without the need for getInstance().</p>
<p>var instance1 = new Singleton();<br />
var instance2 = new Singleton();</p>
<p>alert(instance1 == instance2); // True</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-475</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Fri, 16 May 2008 19:29:11 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-475</guid>
		<description>BK really needs to use &quot;var CatNamesConstructor = ...&quot; so you cannot instantiate CatNamesConstructor in global scope.

Personally, I&#039;ve always preferred:

var CatNames = new function(){
    var privateVar;

    var privateMethod = function(){
        ...
    };

    this.publicMethod = function(){
        ...
    };
};

Notice the &quot;new function(){...}&quot;. Still a little tacky but works quite well.</description>
		<content:encoded><![CDATA[<p>BK really needs to use &#8220;var CatNamesConstructor = &#8230;&#8221; so you cannot instantiate CatNamesConstructor in global scope.</p>
<p>Personally, I&#8217;ve always preferred:</p>
<p>var CatNames = new function(){<br />
    var privateVar;</p>
<p>    var privateMethod = function(){<br />
        &#8230;<br />
    };</p>
<p>    this.publicMethod = function(){<br />
        &#8230;<br />
    };<br />
};</p>
<p>Notice the &#8220;new function(){&#8230;}&#8221;. Still a little tacky but works quite well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/comment-page-1/#comment-76</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Fri, 18 May 2007 23:37:46 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=21#comment-76</guid>
		<description>@troels, this is more and more my point of view on singletons too! Singleton = fancier global. I saw a project in which almost every object was a singleton and it just looked ugly...</description>
		<content:encoded><![CDATA[<p>@troels, this is more and more my point of view on singletons too! Singleton = fancier global. I saw a project in which almost every object was a singleton and it just looked ugly&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
