<?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: JavaScript Programming Patterns</title>
	<atom:link href="http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 25 Jan 2012 00:41:47 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Klaus</title>
		<link>http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/comment-page-1/#comment-31880</link>
		<dc:creator>Klaus</dc:creator>
		<pubDate>Thu, 15 Apr 2010 15:23:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/#comment-31880</guid>
		<description>Hi Jorge,

The problem here is not the Module Pattern, but rather that whenever you use setTimeout to call a function after a certain period of time has elapsed, this function gets executed in the &#039;window&#039; context. In other words, inside that function, the keyword &#039;this&#039; will always reference the global window object.

If you would, instead, call pubFun2 directly, &#039;this&#039; would point to NS1, like:

&lt;pre&gt;&lt;code&gt;NS1.pubFun2(); // will execute pubFun1 from within pubFun2&lt;/code&gt;&lt;/pre&gt;

If you have or absolutely want to use setTimeout, you can use &#039;call&#039;:

&lt;pre&gt;&lt;code&gt;setTimeout(function() { NS1.pubFun2.call(NS1); } , 1000);&lt;/code&gt;&lt;/pre&gt;

The &#039;call&#039; function lets you call any function in a predefined scope, so the statement above essentially means:

&quot;Execute the function pubFun2 after a delay of 1000 milliseconds and make the &#039;this&#039; keyword inside that function reference NS1&quot;.

&lt;a href=&quot;https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Function/call&quot; rel=&quot;nofollow&quot;&gt;MDC&lt;/a&gt; provides some further explanation about call.

I hope that makes sense.

Klaus</description>
		<content:encoded><![CDATA[<p>Hi Jorge,</p>
<p>The problem here is not the Module Pattern, but rather that whenever you use setTimeout to call a function after a certain period of time has elapsed, this function gets executed in the &#8216;window&#8217; context. In other words, inside that function, the keyword &#8216;this&#8217; will always reference the global window object.</p>
<p>If you would, instead, call pubFun2 directly, &#8216;this&#8217; would point to NS1, like:</p>
<pre><code>NS1.pubFun2(); // will execute pubFun1 from within pubFun2</code></pre>
<p>If you have or absolutely want to use setTimeout, you can use &#8216;call&#8217;:</p>
<pre><code>setTimeout(function() { NS1.pubFun2.call(NS1); } , 1000);</code></pre>
<p>The &#8216;call&#8217; function lets you call any function in a predefined scope, so the statement above essentially means:</p>
<p>&#8220;Execute the function pubFun2 after a delay of 1000 milliseconds and make the &#8216;this&#8217; keyword inside that function reference NS1&#8243;.</p>
<p><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Function/call" rel="nofollow">MDC</a> provides some further explanation about call.</p>
<p>I hope that makes sense.</p>
<p>Klaus</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jorge Cercas</title>
		<link>http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/comment-page-1/#comment-31819</link>
		<dc:creator>Jorge Cercas</dc:creator>
		<pubDate>Wed, 14 Apr 2010 17:22:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/#comment-31819</guid>
		<description>I find that using the module pattern and referencing &quot;this&quot; can be problematic depending on who is calling the functions, e.g.

var NS1 = function() {
    var privFun = function() {...};
    return  {
      pubFun1 : function() {...},
      pubFun2 : function() {
        this.pubFun1();
        ...
      }
    };
  }();

setTimeout(NS1.pubFun2, 1000); //ERROR: this.pubFun1 not a function.

So I tend to do it like this...

var NS1 = function() {
    var privFun = function() {...};
    var me = {
      pubFun1 : function() {...},
      pubFun2 : function() {
        me.pubFun1();
        ...
      }
    };
    return me;
  }();
setTimeout(NS1.pubFun2, 1000); //OK

What are your thoughts on this (advantages/disadvantages) and is there another way of doing the same thing?</description>
		<content:encoded><![CDATA[<p>I find that using the module pattern and referencing &#8220;this&#8221; can be problematic depending on who is calling the functions, e.g.</p>
<p>var NS1 = function() {<br />
    var privFun = function() {&#8230;};<br />
    return  {<br />
      pubFun1 : function() {&#8230;},<br />
      pubFun2 : function() {<br />
        this.pubFun1();<br />
        &#8230;<br />
      }<br />
    };<br />
  }();</p>
<p>setTimeout(NS1.pubFun2, 1000); //ERROR: this.pubFun1 not a function.</p>
<p>So I tend to do it like this&#8230;</p>
<p>var NS1 = function() {<br />
    var privFun = function() {&#8230;};<br />
    var me = {<br />
      pubFun1 : function() {&#8230;},<br />
      pubFun2 : function() {<br />
        me.pubFun1();<br />
        &#8230;<br />
      }<br />
    };<br />
    return me;<br />
  }();<br />
setTimeout(NS1.pubFun2, 1000); //OK</p>
<p>What are your thoughts on this (advantages/disadvantages) and is there another way of doing the same thing?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Greenhaw</title>
		<link>http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/comment-page-1/#comment-11607</link>
		<dc:creator>Greg Greenhaw</dc:creator>
		<pubDate>Tue, 07 Oct 2008 22:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.klauskomenda.com/archives/2007/07/07/javascript-programming-patterns/#comment-11607</guid>
		<description>thanks for the article. You can also easily clone a singleton object with this function.


clone: function (arr) 
	{
		if(typeof(arr)==&#039;null&#039; &#124;&#124; typeof(arr)==&#039;undefined&#039;) 
		 	return new Object();
		 	
		 	
		var i, _i, temp;
	    if(typeof arr !== &#039;object&#039;) 
	    	return arr;
	    
	    else 
	    {
	        if(arr.concat) 
	        {
	        	temp = [];
	            for(i = 0, _i = arr.length; i &lt; _i; i++)
	            	temp[i] = arguments.callee(arr[i]);
	        }
	        else 
	        {
	        	temp = {};
	            for(i in arr)
	            	temp[i] = arguments.callee(arr[i]);
	                   
	        }
	     return temp;
	    }
	}</description>
		<content:encoded><![CDATA[<p>thanks for the article. You can also easily clone a singleton object with this function.</p>
<p>clone: function (arr)<br />
	{<br />
		if(typeof(arr)==&#8217;null&#8217; || typeof(arr)==&#8217;undefined&#8217;)<br />
		 	return new Object();</p>
<p>		var i, _i, temp;<br />
	    if(typeof arr !== &#8216;object&#8217;)<br />
	    	return arr;</p>
<p>	    else<br />
	    {<br />
	        if(arr.concat)<br />
	        {<br />
	        	temp = [];<br />
	            for(i = 0, _i = arr.length; i &lt; _i; i++)<br />
	            	temp[i] = arguments.callee(arr[i]);<br />
	        }<br />
	        else<br />
	        {<br />
	        	temp = {};<br />
	            for(i in arr)<br />
	            	temp[i] = arguments.callee(arr[i]);</p>
<p>	        }<br />
	     return temp;<br />
	    }<br />
	}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

