<?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>Thu, 29 Jul 2010 17:07:23 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<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>
	<item>
		<title>By: Ans</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-6184</link>
		<dc:creator>Ans</dc:creator>
		<pubDate>Mon, 23 Nov 2009 21:00:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-6184</guid>
		<description>Hi Daniel,

If I understand the question correctly, it&#039;s the variable. Notice &quot;new Singleton();&quot; and &quot;Singleton.getInstance();&quot; won&#039;t work if the variable name is changed, however, they don&#039;t care if the constructor function name is changed (as long as it is consistent within the function definition itself).</description>
		<content:encoded><![CDATA[<p>Hi Daniel,</p>
<p>If I understand the question correctly, it&#8217;s the variable. Notice &#8220;new Singleton();&#8221; and &#8220;Singleton.getInstance();&#8221; won&#8217;t work if the variable name is changed, however, they don&#8217;t care if the constructor function name is changed (as long as it is consistent within the function definition itself).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://blog.anselmbradford.com/2009/04/21/object-oriented-javascript-tip-the-quintessential-singleton/comment-page-1/#comment-6182</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Mon, 23 Nov 2009 20:22:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=784#comment-6182</guid>
		<description>in the line &quot;var Singleton = new function Singleton() &quot;

when Singleton.getInstance is called,  is the variable or function Singleton being dereferenced?</description>
		<content:encoded><![CDATA[<p>in the line &#8220;var Singleton = new function Singleton() &#8221;</p>
<p>when Singleton.getInstance is called,  is the variable or function Singleton being dereferenced?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
