<?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: Object-Oriented JavaScript Tip: Implementing the Singleton Pattern</title>
	<atom:link href="http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/</link>
	<description>wrangling the Internet's wildest</description>
	<lastBuildDate>Fri, 16 Dec 2011 14:20:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: Ans</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-24083</link>
		<dc:creator>Ans</dc:creator>
		<pubDate>Tue, 26 Jul 2011 16:36:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-24083</guid>
		<description>Nice work! Thanks for sharing.</description>
		<content:encoded><![CDATA[<p>Nice work! Thanks for sharing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rafael</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-24081</link>
		<dc:creator>Rafael</dc:creator>
		<pubDate>Tue, 26 Jul 2011 16:14:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-24081</guid>
		<description>I found a way to do it...

var Teste = (function()
{
    var instance = null;
    var TesteClass = function()
    {
        this.texto = &quot;texto&quot;;
    };

    return new function()
    {
        this.getInstance = function()
        {
            if (instance == null)
                instance = new TesteClass();

            return instance;
        };
    };
})();
var t = Teste.getInstance();
console.log(&quot;t: &quot; + t.texto);
t.texto = &quot;Testando&quot;;
var x = Teste.getInstance();
console.log(&quot;x: &quot; + x.texto);


It works, and I can&#039;t call &quot;new Teste()&quot;, it returns an error.</description>
		<content:encoded><![CDATA[<p>I found a way to do it&#8230;</p>
<p>var Teste = (function()<br />
{<br />
    var instance = null;<br />
    var TesteClass = function()<br />
    {<br />
        this.texto = &#8220;texto&#8221;;<br />
    };</p>
<p>    return new function()<br />
    {<br />
        this.getInstance = function()<br />
        {<br />
            if (instance == null)<br />
                instance = new TesteClass();</p>
<p>            return instance;<br />
        };<br />
    };<br />
})();<br />
var t = Teste.getInstance();<br />
console.log(&#8220;t: &#8221; + t.texto);<br />
t.texto = &#8220;Testando&#8221;;<br />
var x = Teste.getInstance();<br />
console.log(&#8220;x: &#8221; + x.texto);</p>
<p>It works, and I can&#8217;t call &#8220;new Teste()&#8221;, it returns an error.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ans</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-24077</link>
		<dc:creator>Ans</dc:creator>
		<pubDate>Tue, 26 Jul 2011 14:56:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-24077</guid>
		<description>Hi Rafael, 

Off the top of my head you could have this: 

&lt;code&gt;
var instance;
		var Teste = new function Teste()
		{
			if (!instance) instance = this;
			
			Teste.getInstance = function()
			{
				return instance;
			};
			
			this.getInstance = function()
			{
				return instance;
			};
		
			this.texto = &quot;Texto&quot;;
		
			return Teste;
		};
&lt;/code&gt;

Which would work with the rest of your code. It&#039;s not so self-contained though.</description>
		<content:encoded><![CDATA[<p>Hi Rafael, </p>
<p>Off the top of my head you could have this: </p>
<p><code><br />
var instance;<br />
		var Teste = new function Teste()<br />
		{<br />
			if (!instance) instance = this;</p>
<p>			Teste.getInstance = function()<br />
			{<br />
				return instance;<br />
			};</p>
<p>			this.getInstance = function()<br />
			{<br />
				return instance;<br />
			};</p>
<p>			this.texto = "Texto";</p>
<p>			return Teste;<br />
		};<br />
</code></p>
<p>Which would work with the rest of your code. It&#8217;s not so self-contained though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rafael</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-24076</link>
		<dc:creator>Rafael</dc:creator>
		<pubDate>Tue, 26 Jul 2011 14:47:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-24076</guid>
		<description>I understood, I just though that this could be fixed in the function, thx for the help.

Explaning, I&#039;m trying to find a Singleton that won&#039;t have multiple instances, even if the &quot;user&quot; try to hack it.</description>
		<content:encoded><![CDATA[<p>I understood, I just though that this could be fixed in the function, thx for the help.</p>
<p>Explaning, I&#8217;m trying to find a Singleton that won&#8217;t have multiple instances, even if the &#8220;user&#8221; try to hack it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ans</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-24075</link>
		<dc:creator>Ans</dc:creator>
		<pubDate>Tue, 26 Jul 2011 14:43:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-24075</guid>
		<description>Hi Rafael,

This code doesn&#039;t prevent you from creating multiple instances of the singleton if you want to, it just provides a pattern to avoid doing that. In your code you have:

&lt;code&gt;var x = Teste.getInstance();&lt;/code&gt;

...and...

&lt;code&gt;var t = new Teste();&lt;/code&gt;

Which means &lt;code&gt;var x&lt;/code&gt; and &lt;code&gt;var t&lt;/code&gt; are now two different instances, making your comparison equal &lt;code&gt;false&lt;/code&gt;. Also, you have the code:

&lt;code&gt;var y = t.getInstance();&lt;/code&gt; 

...which won&#039;t do, as you are trying to access a method &lt;code&gt;getInstance()&lt;/code&gt; on the &lt;code&gt;Teste&lt;/code&gt; object assigned to &lt;code&gt;t&lt;/code&gt;. The object &lt;code&gt;t&lt;/code&gt; does not have that method as the &lt;code&gt;getInstance()&lt;/code&gt; method is a static method attached to the class &lt;code&gt;Teste&lt;/code&gt;, not an instance of that class. So to fix this, you&#039;d need to eliminate &lt;code&gt;var t&lt;/code&gt; and change &lt;code&gt;var y&lt;/code&gt; to:

&lt;code&gt;var y = Teste.getInstance();&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Hi Rafael,</p>
<p>This code doesn&#8217;t prevent you from creating multiple instances of the singleton if you want to, it just provides a pattern to avoid doing that. In your code you have:</p>
<p><code>var x = Teste.getInstance();</code></p>
<p>&#8230;and&#8230;</p>
<p><code>var t = new Teste();</code></p>
<p>Which means <code>var x</code> and <code>var t</code> are now two different instances, making your comparison equal <code>false</code>. Also, you have the code:</p>
<p><code>var y = t.getInstance();</code> </p>
<p>&#8230;which won&#8217;t do, as you are trying to access a method <code>getInstance()</code> on the <code>Teste</code> object assigned to <code>t</code>. The object <code>t</code> does not have that method as the <code>getInstance()</code> method is a static method attached to the class <code>Teste</code>, not an instance of that class. So to fix this, you&#8217;d need to eliminate <code>var t</code> and change <code>var y</code> to:</p>
<p><code>var y = Teste.getInstance();</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rafael</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-24074</link>
		<dc:creator>Rafael</dc:creator>
		<pubDate>Tue, 26 Jul 2011 14:23:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-24074</guid>
		<description>I found a bug on it....


var Teste = new function Teste()
{
    var instance = this;
    Teste.getInstance = function()
    {
        return instance;
    };

    this.texto = &quot;Texto&quot;;

    return Teste;
};


var x = Teste.getInstance();
x.texto = &quot;Testando&quot;;
var t = new Teste();
var y = t.getInstance();
console.log( x == y );

It will return false.....
x.texto = Testando
y.texto = Teste


Can I somehow fix it?</description>
		<content:encoded><![CDATA[<p>I found a bug on it&#8230;.</p>
<p>var Teste = new function Teste()<br />
{<br />
    var instance = this;<br />
    Teste.getInstance = function()<br />
    {<br />
        return instance;<br />
    };</p>
<p>    this.texto = &#8220;Texto&#8221;;</p>
<p>    return Teste;<br />
};</p>
<p>var x = Teste.getInstance();<br />
x.texto = &#8220;Testando&#8221;;<br />
var t = new Teste();<br />
var y = t.getInstance();<br />
console.log( x == y );</p>
<p>It will return false&#8230;..<br />
x.texto = Testando<br />
y.texto = Teste</p>
<p>Can I somehow fix it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Papineau</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-17851</link>
		<dc:creator>Jeff Papineau</dc:creator>
		<pubDate>Thu, 03 Feb 2011 18:23:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-17851</guid>
		<description>I like being able to include private vars, I prefer the named object signature for singletons sometimes.</description>
		<content:encoded><![CDATA[<p>I like being able to include private vars, I prefer the named object signature for singletons sometimes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: simon</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-17432</link>
		<dc:creator>simon</dc:creator>
		<pubDate>Mon, 24 Jan 2011 11:43:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-17432</guid>
		<description>&quot;no need for a class-like constructor for an object.
object literals is all you need.&quot;

Douglas CrockFord,
senior JavaScript architect</description>
		<content:encoded><![CDATA[<p>&#8220;no need for a class-like constructor for an object.<br />
object literals is all you need.&#8221;</p>
<p>Douglas CrockFord,<br />
senior JavaScript architect</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fred</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-10577</link>
		<dc:creator>fred</dc:creator>
		<pubDate>Sun, 28 Mar 2010 16:01:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-10577</guid>
		<description>Why not just using 

&lt;pre lang=&quot;javascript&quot;&gt;
Singleton = function() {
// private vars and functions
return {
// public vars and functions

}();
&lt;/pre&gt;

When the code run, the Singleton is automatically instantied an could not be instantied a second time
(except if used with var Second = new Singleton.constructor())



}();</description>
		<content:encoded><![CDATA[<p>Why not just using</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Singleton <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// private vars and functions</span>
<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// public vars and functions</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When the code run, the Singleton is automatically instantied an could not be instantied a second time<br />
(except if used with var Second = new Singleton.constructor())</p>
<p>}();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-10567</link>
		<dc:creator>Anthony</dc:creator>
		<pubDate>Sun, 28 Mar 2010 08:15:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-10567</guid>
		<description>It bothers me that (Class.Singleton() !== (new Class())) so I figured out how to stop instantiation of the class without disrupting the way it works... here it is:

&lt;pre lang=&quot;javascript&quot;&gt;
var Test = new function Test()
{
	if ( Test.initialized )
		throw new Error(&#039;You may not instantiate Test&#039;);
	
	var instance = this;
	
	Test.Singleton = function() { return instance; };
	return Test;
};
Test.initialized = true;
Test.prototype.data = &#039;some data&#039;;
		
var obj = Test.Singleton();
alert(obj.data);
obj.data = &#039;test&#039;;
alert(obj.data);

var other = Test.Singleton();
alert(other.data);

var fail = new Test;
alert(fail.data);
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>It bothers me that (Class.Singleton() !== (new Class())) so I figured out how to stop instantiation of the class without disrupting the way it works&#8230; here it is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Test <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">function</span> Test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Test.<span style="color: #660066;">initialized</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You may not instantiate Test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> instance <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
	Test.<span style="color: #660066;">Singleton</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> instance<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> Test<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Test.<span style="color: #660066;">initialized</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
Test.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'some data'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> Test.<span style="color: #660066;">Singleton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
obj.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'test'</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> other <span style="color: #339933;">=</span> Test.<span style="color: #660066;">Singleton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>other.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> fail <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Test<span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>fail.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
	</item>
</channel>
</rss>

