<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Javascript Design Patterns - 1. The Singleton</title>
	<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/</link>
	<description>prototypical scriptings all over the place</description>
	<pubDate>Wed, 08 Sep 2010 01:59:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: Dan</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-9427</link>
		<author>Dan</author>
		<pubDate>Wed, 14 Apr 2010 11:41:09 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-9427</guid>
		<description>&lt;blockquote&gt;We learned (or already knew) that objects are the return of &lt;b&gt;a&lt;/b&gt; function call (such as new Object();).&lt;/blockquote&gt;

&lt;p&gt;You can't say it like that. That's only true for your specific example right here. Also when using 'new' you're technically not issuing a function call, but really instantiating an object.&lt;/p&gt;

&lt;p&gt;I don't want to nitpick, but that statement surely will confuse people.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<blockquote><p>We learned (or already knew) that objects are the return of <b>a</b> function call (such as new Object();).</p></blockquote>
<p>You can&#8217;t say it like that. That&#8217;s only true for your specific example right here. Also when using &#8216;new&#8217; you&#8217;re technically not issuing a function call, but really instantiating an object.</p>
<p>I don&#8217;t want to nitpick, but that statement surely will confuse people.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: _private - StartTags.com</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-9332</link>
		<author>_private - StartTags.com</author>
		<pubDate>Tue, 02 Mar 2010 22:28:47 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-9332</guid>
		<description>&lt;p&gt;[...] not be published) (required) Website. Copyright &#169; 2004-09 Dagon Design - WordPress Powered ...prototyp.ical.ly Javascript Design Patterns - 1. The SingletonThis being the first part of a series of posts that I plan to write (hopefully) I will provide bits [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[&#8230;] not be published) (required) Website. Copyright &copy; 2004-09 Dagon Design - WordPress Powered &#8230;prototyp.ical.ly Javascript Design Patterns - 1. The SingletonThis being the first part of a series of posts that I plan to write (hopefully) I will provide bits [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Cochran</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-9238</link>
		<author>Samuel Cochran</author>
		<pubDate>Mon, 01 Feb 2010 06:46:35 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-9238</guid>
		<description>&lt;p&gt;Javascript has no classes (it is a prototypical language) so you don't create singletons. Achieve the same effect by creating an object with properties and methods:&lt;/p&gt;

&lt;p&gt;var Singleton = {
    one: 1,
    two: function() {
        return this.one + 1;
    }
};&lt;/p&gt;

&lt;p&gt;alert(Singleton.one);
alert(Singleton.two());&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Javascript has no classes (it is a prototypical language) so you don&#8217;t create singletons. Achieve the same effect by creating an object with properties and methods:</p>
<p>var Singleton = {<br />
    one: 1,<br />
    two: function() {<br />
        return this.one + 1;<br />
    }<br />
};</p>
<p>alert(Singleton.one);<br />
alert(Singleton.two());</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-8309</link>
		<author>David</author>
		<pubDate>Sun, 09 Aug 2009 14:11:47 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-8309</guid>
		<description>&lt;p&gt;Singleton = function() {
    if (! Singleton.prototype.instance) {
        Singleton.prototype.instance = this;
    } else {
        return Singleton.prototype.instance;
    }
    // Private &#38; Privileged
}&lt;/p&gt;

&lt;p&gt;Singleton.prototype = {
    // Public
}&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Singleton = function() {<br />
    if (! Singleton.prototype.instance) {<br />
        Singleton.prototype.instance = this;<br />
    } else {<br />
        return Singleton.prototype.instance;<br />
    }<br />
    // Private &amp; Privileged<br />
}</p>
<p>Singleton.prototype = {<br />
    // Public<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fatbrain</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-2292</link>
		<author>fatbrain</author>
		<pubDate>Sat, 24 May 2008 12:55:14 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-2292</guid>
		<description>&lt;p&gt;The simplest way, and most accurate is this (imo):&lt;/p&gt;

&lt;p&gt;Consider:&lt;/p&gt;

&lt;p&gt;var singelton = (function() { function MyObj(...) { ... }; ...; var o = new MyObj(...); o.constructor = null; return o; })();&lt;/p&gt;

&lt;p&gt;It will also inhibit the user from creating more instances from new some_object.constructor(...)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The simplest way, and most accurate is this (imo):</p>
<p>Consider:</p>
<p>var singelton = (function() { function MyObj(&#8230;) { &#8230; }; &#8230;; var o = new MyObj(&#8230;); o.constructor = null; return o; })();</p>
<p>It will also inhibit the user from creating more instances from new some_object.constructor(&#8230;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ext Javascript Library for beginners, ext-perience Ext &#187; Blog Archive &#187; Javascript Singleton Pattern explained</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-1946</link>
		<author>Ext Javascript Library for beginners, ext-perience Ext &#187; Blog Archive &#187; Javascript Singleton Pattern explained</author>
		<pubDate>Wed, 02 Apr 2008 03:14:17 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-1946</guid>
		<description>&lt;p&gt;[...] a great site with an explanation of the Singleton design pattern in Javascript with [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[&#8230;] a great site with an explanation of the Singleton design pattern in Javascript with [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Al</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-1695</link>
		<author>Al</author>
		<pubDate>Tue, 04 Mar 2008 22:14:38 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-1695</guid>
		<description>&lt;p&gt;Thanks! Exactly what I'm looking for!  Nice example.  I too dread Javascript 2.0 even though it provides more OO features.   The toolkit I use to develop JS utilizes similar coding style to yours and I've grown use to it.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks! Exactly what I&#8217;m looking for!  Nice example.  I too dread Javascript 2.0 even though it provides more OO features.   The toolkit I use to develop JS utilizes similar coding style to yours and I&#8217;ve grown use to it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Foley</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-1112</link>
		<author>David Foley</author>
		<pubDate>Fri, 23 Nov 2007 23:03:57 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-1112</guid>
		<description>&lt;p&gt;Hi Darren - didnt spot my post being published till now (talk about tardiness!) - actually its possible - rereading my post I realised my typos - this the correct version:&lt;/p&gt;

&lt;p&gt;function provideSingleton (oSuper)
{
    return (function()
    {
        var _instance = null;
        return {
            getInstance: function()
            {
                if(_instance == null) _instance = new oSuper();
                return _instance;
            }
        };
    })();
}&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Darren - didnt spot my post being published till now (talk about tardiness!) - actually its possible - rereading my post I realised my typos - this the correct version:</p>
<p>function provideSingleton (oSuper)<br />
{<br />
    return (function()<br />
    {<br />
        var _instance = null;<br />
        return {<br />
            getInstance: function()<br />
            {<br />
                if(_instance == null) _instance = new oSuper();<br />
                return _instance;<br />
            }<br />
        };<br />
    })();<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Darren Hurley</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-700</link>
		<author>Darren Hurley</author>
		<pubDate>Fri, 31 Aug 2007 14:43:46 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-700</guid>
		<description>&lt;p&gt;@David, I'm not sure that would work.&lt;/p&gt;

&lt;p&gt;Firstly, what's stopping someone creating another MyClass object by just calling new MyClass() as many times as they want?&lt;/p&gt;

&lt;p&gt;Also, if I understand this right, the provideSingleton function returns an object which references the variable _private._instance in the containing function. But after the first time you call getInstance, this _private._instance var will be updated, never again equal to null, i.e. you can only call getInstance once.
I think the point your missing is there is only one variable _private.&lt;/p&gt;

&lt;p&gt;(on another note, the way your initailizing this variable's a bit wrong. This would be better
&lt;code&gt;var _private = {};
_private._instance = null&lt;/code&gt;
)&lt;/p&gt;

&lt;p&gt;You're right about being able to create singleton's dynamically, but i don't think that's possible in JavaScript&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@David, I&#8217;m not sure that would work.</p>
<p>Firstly, what&#8217;s stopping someone creating another MyClass object by just calling new MyClass() as many times as they want?</p>
<p>Also, if I understand this right, the provideSingleton function returns an object which references the variable _private._instance in the containing function. But after the first time you call getInstance, this _private._instance var will be updated, never again equal to null, i.e. you can only call getInstance once.<br />
I think the point your missing is there is only one variable _private.</p>
<p>(on another note, the way your initailizing this variable&#8217;s a bit wrong. This would be better<br />
<code>var _private = {};<br />
_private._instance = null</code><br />
)</p>
<p>You&#8217;re right about being able to create singleton&#8217;s dynamically, but i don&#8217;t think that&#8217;s possible in JavaScript</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Foley</title>
		<link>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-210</link>
		<author>David Foley</author>
		<pubDate>Wed, 25 Apr 2007 19:47:09 +0000</pubDate>
		<guid>http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/#comment-210</guid>
		<description>&lt;p&gt;Christian - not a critique per se, but isn't a Singleton a Singleton by merit of not requiring explicit instantiation (i.e. new Singleton() outside of the 'class' definition ) ? I looked into the area and came up  this a few months ago. Its short and sweet and doesnt extend native objects
&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;function provideSingleton ( oSuper ) {
        var _private {_instance: null;}
        return {
        getInstance: function () {
            if ( _private._instance == null ) _private._instance = new oSuper ();
            return _private._instance;
        }
    };&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;To use it, just provide a JavaScript class definition to the function, and call it with the getInstance method, like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;// rough and ready class def
 function MyClass () {};
MyClass.prototype.instanceProperty = "instanceProperty";
MyClass.prototype.instanceMethod = function () {
     return this.instanceProperty;
};&lt;/p&gt;

&lt;p&gt;var SingletonMyClass = provideSingleton ( MyClass );&lt;/p&gt;

&lt;p&gt;var SingletonReference = SingeltonMyClass.getInstance();&lt;/p&gt;

&lt;p&gt;SingletonMyClass.getInstance().instanceMethod() // returns 'instance property'&lt;/p&gt;

&lt;p&gt;SingeltonReference.instanceMethod() // returns 'instance property'&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Christian - not a critique per se, but isn&#8217;t a Singleton a Singleton by merit of not requiring explicit instantiation (i.e. new Singleton() outside of the &#8216;class&#8217; definition ) ? I looked into the area and came up  this a few months ago. Its short and sweet and doesnt extend native objects<br />
<code></code></p>
<p>function provideSingleton ( oSuper ) {<br />
        var _private {_instance: null;}<br />
        return {<br />
        getInstance: function () {<br />
            if ( _private._instance == null ) _private._instance = new oSuper ();<br />
            return _private._instance;<br />
        }<br />
    };</p>
</p>
<p>To use it, just provide a JavaScript class definition to the function, and call it with the getInstance method, like this:</p>
<p><code></code></p>
<p>// rough and ready class def<br />
 function MyClass () {};<br />
MyClass.prototype.instanceProperty = &#8220;instanceProperty&#8221;;<br />
MyClass.prototype.instanceMethod = function () {<br />
     return this.instanceProperty;<br />
};</p>
<p>var SingletonMyClass = provideSingleton ( MyClass );</p>
<p>var SingletonReference = SingeltonMyClass.getInstance();</p>
<p>SingletonMyClass.getInstance().instanceMethod() // returns &#8216;instance property&#8217;</p>
<p>SingeltonReference.instanceMethod() // returns &#8216;instance property&#8217;</p></p>
]]></content:encoded>
	</item>
</channel>
</rss>
