<?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: A JavaScript Module Pattern</title>
	<atom:link href="http://ajaxian.com/archives/a-javascript-module-pattern/feed" rel="self" type="application/rss+xml" />
	<link>http://ajaxian.com/archives/a-javascript-module-pattern</link>
	<description>Cleaning up the web with Ajax</description>
	<lastBuildDate>Thu, 09 Feb 2012 06:55:33 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: saeedneamati</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-285947</link>
		<dc:creator>saeedneamati</dc:creator>
		<pubDate>Mon, 17 Oct 2011 06:58:36 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-285947</guid>
		<description>This pattern is one of the most useful patterns I&#039;ve ever learnt. I truly respect Douglas Crockford as a good teacher, and thanks Aajxian for this great post.</description>
		<content:encoded><![CDATA[<p>This pattern is one of the most useful patterns I&#8217;ve ever learnt. I truly respect Douglas Crockford as a good teacher, and thanks Aajxian for this great post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: satya61229</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-285846</link>
		<dc:creator>satya61229</dc:creator>
		<pubDate>Fri, 22 Jul 2011 08:24:48 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-285846</guid>
		<description>I find it better and more useful than what was discussed at the YUI blog. Thanks</description>
		<content:encoded><![CDATA[<p>I find it better and more useful than what was discussed at the YUI blog. Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: loige</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-276889</link>
		<dc:creator>loige</dc:creator>
		<pubDate>Sat, 28 Nov 2009 07:46:22 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-276889</guid>
		<description>How does private methods can call public members?</description>
		<content:encoded><![CDATA[<p>How does private methods can call public members?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wilq32</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-273339</link>
		<dc:creator>wilq32</dc:creator>
		<pubDate>Wed, 06 May 2009 08:20:11 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-273339</guid>
		<description>On mine page there is other concept of getting private members... Just check it and tell me what you think.

http://wilq32.googlepages.com/wilq32.class</description>
		<content:encoded><![CDATA[<p>On mine page there is other concept of getting private members&#8230; Just check it and tell me what you think.</p>
<p><a href="http://wilq32.googlepages.com/wilq32.class" rel="nofollow">http://wilq32.googlepages.com/wilq32.class</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: richtaur</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-265320</link>
		<dc:creator>richtaur</dc:creator>
		<pubDate>Sun, 22 Jun 2008 07:48:30 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-265320</guid>
		<description>I can&#039;t stand looking at my old code that doesn&#039;t use any of these principles. This has completely changed the way I write JavaScript.</description>
		<content:encoded><![CDATA[<p>I can&#8217;t stand looking at my old code that doesn&#8217;t use any of these principles. This has completely changed the way I write JavaScript.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Topper</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-254483</link>
		<dc:creator>Topper</dc:creator>
		<pubDate>Sun, 26 Aug 2007 02:08:15 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-254483</guid>
		<description>I made a syntactically nice version of this with support for instances as well.  Check it out:  http://blog.quimble.com/?p=114 (is the writeup).  

http://wiki.s2807.gridserver.com/JavascriptTechnique  is the code.</description>
		<content:encoded><![CDATA[<p>I made a syntactically nice version of this with support for instances as well.  Check it out:  <a href="http://blog.quimble.com/?p=114" rel="nofollow">http://blog.quimble.com/?p=114</a> (is the writeup).  </p>
<p><a href="http://wiki.s2807.gridserver.com/JavascriptTechnique" rel="nofollow">http://wiki.s2807.gridserver.com/JavascriptTechnique</a>  is the code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garrett</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251577</link>
		<dc:creator>Garrett</dc:creator>
		<pubDate>Fri, 15 Jun 2007 00:51:29 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251577</guid>
		<description>&gt; It also teaches the idea of closure and automatically evaluated functions which is very useful.

Closures are powerful, useful, yes. Necessary here? hardly. 

Knowing when not to use one is as important as knowing what a closure is. 

&gt;It can keep the global name space less cluttered.
Singleton (and this bastardized version) does little to keep the global namespace clean, though this is not really relevant to the javascript/web-paradigm, where there is library code and implementation code.

In fact, if your main goal is to keep the global nameppace clean, use a &lt;a href=&quot;http://butunclebob.com/ArticleS.UncleBob.SingletonVsJustCreateOne&quot; rel=&quot;nofollow&quot;&gt;just 
create one&lt;/a&gt; approach (hardly necessary for most JS).

There is a much better way to write a Singleton using lazy-load approach and enforcing an entryPoint to the constructor. 

One more problem with the &quot;power constructor&quot;: There is extra memory allocation and worse performance due to closed scope. References to inner function objects are assigned to to the public properties of a constructed object. 
 
Closures should be used where they are needed and ONLY where they are needed. For example: event registry/broadcast adapter/change manager/notification system, or adding advice (not generally recommended). Closure is hardly necessary here.</description>
		<content:encoded><![CDATA[<p>&gt; It also teaches the idea of closure and automatically evaluated functions which is very useful.</p>
<p>Closures are powerful, useful, yes. Necessary here? hardly. </p>
<p>Knowing when not to use one is as important as knowing what a closure is. </p>
<p>&gt;It can keep the global name space less cluttered.<br />
Singleton (and this bastardized version) does little to keep the global namespace clean, though this is not really relevant to the javascript/web-paradigm, where there is library code and implementation code.</p>
<p>In fact, if your main goal is to keep the global nameppace clean, use a <a href="http://butunclebob.com/ArticleS.UncleBob.SingletonVsJustCreateOne" rel="nofollow">just<br />
create one</a> approach (hardly necessary for most JS).</p>
<p>There is a much better way to write a Singleton using lazy-load approach and enforcing an entryPoint to the constructor. </p>
<p>One more problem with the &#8220;power constructor&#8221;: There is extra memory allocation and worse performance due to closed scope. References to inner function objects are assigned to to the public properties of a constructed object. </p>
<p>Closures should be used where they are needed and ONLY where they are needed. For example: event registry/broadcast adapter/change manager/notification system, or adding advice (not generally recommended). Closure is hardly necessary here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Michaux</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251572</link>
		<dc:creator>Peter Michaux</dc:creator>
		<pubDate>Thu, 14 Jun 2007 19:58:12 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251572</guid>
		<description>Tim,

I think that in many cases design patterns are exactly to add in &quot;missing&quot; features of a language. For example, Smalltalk classes all inherit from a class with Subject and Observer interfaces. Perl has modules and exporting like the pattern described here. C++ has template classes. Java has interfaces. JavaScript has prototype-based inheritance. All of these concepts could be useful at some point in another language where it is missing. People were using classes and inheritance in C before any object-oriented language had sugar for class definition.

I agree that this module pattern in  JavaScript can be gratuitous at times. It doesn&#039;t create indestructible objects with truly private/protected anything. It can keep the global name space less cluttered. It also teaches the idea of closure and automatically evaluated functions which is very useful. I use it relatively frequently when working around browser issues to produce efficient functions that don&#039;t determine the browser-appropriate algorithm each time a function is called.

It is all subjective.</description>
		<content:encoded><![CDATA[<p>Tim,</p>
<p>I think that in many cases design patterns are exactly to add in &#8220;missing&#8221; features of a language. For example, Smalltalk classes all inherit from a class with Subject and Observer interfaces. Perl has modules and exporting like the pattern described here. C++ has template classes. Java has interfaces. JavaScript has prototype-based inheritance. All of these concepts could be useful at some point in another language where it is missing. People were using classes and inheritance in C before any object-oriented language had sugar for class definition.</p>
<p>I agree that this module pattern in  JavaScript can be gratuitous at times. It doesn&#8217;t create indestructible objects with truly private/protected anything. It can keep the global name space less cluttered. It also teaches the idea of closure and automatically evaluated functions which is very useful. I use it relatively frequently when working around browser issues to produce efficient functions that don&#8217;t determine the browser-appropriate algorithm each time a function is called.</p>
<p>It is all subjective.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Scarfe</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251569</link>
		<dc:creator>Tim Scarfe</dc:creator>
		<pubDate>Thu, 14 Jun 2007 18:31:09 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251569</guid>
		<description>Peter,

Actually I am an advocate of design patterns. However patterns are not designed to conceptualise missing features of a language, rather allow developers to communicate using well-known, well understood constructs for software interactions.

I realise I am being ambigious and perhaps what I have just said would back up the pattern used above.

My issue with this pattern is that it&#039;s gratuitous. Patterns for the sake of patterns are bad because they add an unnessecary level of abstraction which only reduces maintainability further. 

I guess this is pretty subjective so don&#039;t take this as a statement, rather my opinion on the matter.

Tim</description>
		<content:encoded><![CDATA[<p>Peter,</p>
<p>Actually I am an advocate of design patterns. However patterns are not designed to conceptualise missing features of a language, rather allow developers to communicate using well-known, well understood constructs for software interactions.</p>
<p>I realise I am being ambigious and perhaps what I have just said would back up the pattern used above.</p>
<p>My issue with this pattern is that it&#8217;s gratuitous. Patterns for the sake of patterns are bad because they add an unnessecary level of abstraction which only reduces maintainability further. </p>
<p>I guess this is pretty subjective so don&#8217;t take this as a statement, rather my opinion on the matter.</p>
<p>Tim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Michaux</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251568</link>
		<dc:creator>Peter Michaux</dc:creator>
		<pubDate>Thu, 14 Jun 2007 18:15:08 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251568</guid>
		<description>Tim, I think you&#039;ve missed the point of Design Patterns in general. Any language will have certain features built-in that will be Design Patterns in another language. For example, C++ has class-based inheritance built-in but prototype-based inheritance is a design pattern (see gang of four). In JavaScript the prototype-based inheritance is built-in and class-based inheritance is a design pattern. To the JavaScripter the prototype-based inheritance design pattern in &quot;an enterprise capable language&quot; will look like a weird pattern pretending to look like Self or JavaScript. So if someone wants to use a module in JavaScript, at least until JavaScript 2, they use a design pattern.</description>
		<content:encoded><![CDATA[<p>Tim, I think you&#8217;ve missed the point of Design Patterns in general. Any language will have certain features built-in that will be Design Patterns in another language. For example, C++ has class-based inheritance built-in but prototype-based inheritance is a design pattern (see gang of four). In JavaScript the prototype-based inheritance is built-in and class-based inheritance is a design pattern. To the JavaScripter the prototype-based inheritance design pattern in &#8220;an enterprise capable language&#8221; will look like a weird pattern pretending to look like Self or JavaScript. So if someone wants to use a module in JavaScript, at least until JavaScript 2, they use a design pattern.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Scarfe</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251548</link>
		<dc:creator>Tim Scarfe</dc:creator>
		<pubDate>Thu, 14 Jun 2007 09:32:03 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251548</guid>
		<description>I love it how JavaScript developers are always coming up with weird patterns and conventions pretending to try and make it behave like an enterprise capable language. Actually they seem to be openly participating in a form of elitism, a close-knit club if you like.
JavaScript is a very capable OOP language, and encapsulation is one of the three tenants of OOP. Some people here say this is a singleton pattern. To me this whole construct should just be a function. I mean seriously... if you think you need private members in JavaScript you are on the wrong track. JavaScript is just a glue language for a presentation tier, no business logic.
Your â€œprivate methodâ€ is actually an anonymous function assigned to a variable inside another function i.e. a closure which should be avoided anyway. This â€œmethodâ€ probably wonâ€™t even be called from the scope of the object instance.
And this module returns an object literal with another anonymous function?!
Excuse me but in middleware the biggest priority should be maintainability and this would seem to be a failure in that respect.</description>
		<content:encoded><![CDATA[<p>I love it how JavaScript developers are always coming up with weird patterns and conventions pretending to try and make it behave like an enterprise capable language. Actually they seem to be openly participating in a form of elitism, a close-knit club if you like.<br />
JavaScript is a very capable OOP language, and encapsulation is one of the three tenants of OOP. Some people here say this is a singleton pattern. To me this whole construct should just be a function. I mean seriously&#8230; if you think you need private members in JavaScript you are on the wrong track. JavaScript is just a glue language for a presentation tier, no business logic.<br />
Your â€œprivate methodâ€ is actually an anonymous function assigned to a variable inside another function i.e. a closure which should be avoided anyway. This â€œmethodâ€ probably wonâ€™t even be called from the scope of the object instance.<br />
And this module returns an object literal with another anonymous function?!<br />
Excuse me but in middleware the biggest priority should be maintainability and this would seem to be a failure in that respect.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garrett</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251542</link>
		<dc:creator>Garrett</dc:creator>
		<pubDate>Thu, 14 Jun 2007 07:16:27 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251542</guid>
		<description>@Jordan
Exactly.

An untyped object with an anonymous constructor. 

YUI loves anonymous functions and I don&#039;t know why. It is a real pain in the neck to debug &quot;anonymous&quot; or &quot;no name&quot; in the debugger.

Now please look at the code and notice that the object&#039;s public interface is returned. All the &quot;public&quot; members are at the very bottom. Is this not completely backwards? 

This is Doug&#039;s &quot;power constructor&quot; renamed. Doug is a nice man and I don&#039;t hate him. It is not a good practice.

The way to enforce an entryPoint is with Function.prototype.caller. This is not very hard to do and makes a private constructor, lazy-load singleton possible. 

Opera chooses to support wacky css instead of critical properties like Function.prototype.caller. Opera should fix this bug. Ian, Anne...

Function.prototype.caller can also be used to debug errors, and even build-your-own error stack by looking up the call chain. (Error.stack is natively supported in FireFox and Webkit) 

Yes, Dustin, a lot of people at Yahoo do in fact use that approach and I am sure that it came from Doug. I have tried to explain my best why it is not good. I hope it makes sense to others.</description>
		<content:encoded><![CDATA[<p>@Jordan<br />
Exactly.</p>
<p>An untyped object with an anonymous constructor. </p>
<p>YUI loves anonymous functions and I don&#8217;t know why. It is a real pain in the neck to debug &#8220;anonymous&#8221; or &#8220;no name&#8221; in the debugger.</p>
<p>Now please look at the code and notice that the object&#8217;s public interface is returned. All the &#8220;public&#8221; members are at the very bottom. Is this not completely backwards? </p>
<p>This is Doug&#8217;s &#8220;power constructor&#8221; renamed. Doug is a nice man and I don&#8217;t hate him. It is not a good practice.</p>
<p>The way to enforce an entryPoint is with Function.prototype.caller. This is not very hard to do and makes a private constructor, lazy-load singleton possible. </p>
<p>Opera chooses to support wacky css instead of critical properties like Function.prototype.caller. Opera should fix this bug. Ian, Anne&#8230;</p>
<p>Function.prototype.caller can also be used to debug errors, and even build-your-own error stack by looking up the call chain. (Error.stack is natively supported in FireFox and Webkit) </p>
<p>Yes, Dustin, a lot of people at Yahoo do in fact use that approach and I am sure that it came from Doug. I have tried to explain my best why it is not good. I hope it makes sense to others.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Trevor</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251541</link>
		<dc:creator>Trevor</dc:creator>
		<pubDate>Thu, 14 Jun 2007 06:12:00 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251541</guid>
		<description>@Ivan: I don&#039;t see how that follows. I think Teun&#039;s question is valid, especially considering one can create private variables within Teun&#039;s myPublicMethod() the same way one could create them within YAHOO.myProject.myModule();</description>
		<content:encoded><![CDATA[<p>@Ivan: I don&#8217;t see how that follows. I think Teun&#8217;s question is valid, especially considering one can create private variables within Teun&#8217;s myPublicMethod() the same way one could create them within YAHOO.myProject.myModule();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dustin Diaz</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251535</link>
		<dc:creator>Dustin Diaz</dc:creator>
		<pubDate>Wed, 13 Jun 2007 21:58:50 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251535</guid>
		<description>@Matthew: Yes. Yes it is. They may have just had an oversight of the post. But to their own benefit, they put the name of &quot;module pattern&quot; to it. It was in fact a pattern that I learned when I was at Y! so it&#039;s all good.</description>
		<content:encoded><![CDATA[<p>@Matthew: Yes. Yes it is. They may have just had an oversight of the post. But to their own benefit, they put the name of &#8220;module pattern&#8221; to it. It was in fact a pattern that I learned when I was at Y! so it&#8217;s all good.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251534</link>
		<dc:creator>Ivan</dc:creator>
		<pubDate>Wed, 13 Jun 2007 21:37:44 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251534</guid>
		<description>Might as well as why you even need objects.</description>
		<content:encoded><![CDATA[<p>Might as well as why you even need objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Teun</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251527</link>
		<dc:creator>Teun</dc:creator>
		<pubDate>Wed, 13 Jun 2007 16:16:12 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251527</guid>
		<description>Can anybody explain the advantages over simple stuff like
[code]
YAHOO.myProject.myModule =
{
  myNotSoPrivateVar: &quot;&quot;,
  myPublicProperty: &quot;&quot;,
  myPublicMethod: function(){}
};
[/code]
why do i want private variables?</description>
		<content:encoded><![CDATA[<p>Can anybody explain the advantages over simple stuff like<br />
[code]<br />
YAHOO.myProject.myModule =<br />
{<br />
  myNotSoPrivateVar: "",<br />
  myPublicProperty: "",<br />
  myPublicMethod: function(){}<br />
};<br />
[/code]<br />
why do i want private variables?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251517</link>
		<dc:creator>Ivan</dc:creator>
		<pubDate>Wed, 13 Jun 2007 13:25:21 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251517</guid>
		<description>I really hope this takes off.  I&#039;ve been doing very similar for a while now, but I definitely feel like a lone man in the sea of functions only JS programming.</description>
		<content:encoded><![CDATA[<p>I really hope this takes off.  I&#8217;ve been doing very similar for a while now, but I definitely feel like a lone man in the sea of functions only JS programming.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas Messier</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251515</link>
		<dc:creator>Thomas Messier</dc:creator>
		<pubDate>Wed, 13 Jun 2007 13:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251515</guid>
		<description>@Jordan

This is meant to be a singleton pattern.</description>
		<content:encoded><![CDATA[<p>@Jordan</p>
<p>This is meant to be a singleton pattern.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jordan</title>
		<link>http://ajaxian.com/archives/a-javascript-module-pattern/comment-page-1#comment-251514</link>
		<dc:creator>Jordan</dc:creator>
		<pubDate>Wed, 13 Jun 2007 12:55:26 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=2483#comment-251514</guid>
		<description>Why are the public methods set directly on the object instead of on the object&#039;s prototype? That means if I have multiple instances of that object, they can&#039;t share the same methods (through the prototype) and each object must carry it&#039;s own method! That is memory intensive!</description>
		<content:encoded><![CDATA[<p>Why are the public methods set directly on the object instead of on the object&#8217;s prototype? That means if I have multiple instances of that object, they can&#8217;t share the same methods (through the prototype) and each object must carry it&#8217;s own method! That is memory intensive!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

