<?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: Is JavaScript&#8217;s &#8220;Global&#8221; Scope Really Just A Closure?</title>
	<atom:link href="http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/</link>
	<description>Better Than Yesterday</description>
	<lastBuildDate>Fri, 24 May 2013 06:39:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Derick Bailey</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2080</link>
		<dc:creator>Derick Bailey</dc:creator>
		<pubDate>Sun, 01 Jan 2012 00:49:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2080</guid>
		<description>yeah, in this version you&#039;re replacing the &quot;write&quot; function on the global &quot;document&quot; object. 

another way to do this is like I said in the comment for your other version... use the &quot;call&quot; method to set the context of your function and then call &quot;this.document.write&quot; in that...

var w = {
  document: {
    write: function(){
      // ...
    }
  }
}

function myFunc(){
  this.document.write(&quot;foo&quot;);
}

myFunc.call(w);

or just pass in an argument to the function:

function func2(w2){
  w2.document.write(&quot;bar&quot;);
}

func2(w);</description>
		<content:encoded><![CDATA[<p>yeah, in this version you&#8217;re replacing the &#8220;write&#8221; function on the global &#8220;document&#8221; object. </p>
<p>another way to do this is like I said in the comment for your other version&#8230; use the &#8220;call&#8221; method to set the context of your function and then call &#8220;this.document.write&#8221; in that&#8230;</p>
<p>var w = {<br />
  document: {<br />
    write: function(){<br />
      // &#8230;<br />
    }<br />
  }<br />
}</p>
<p>function myFunc(){<br />
  this.document.write(&#8220;foo&#8221;);<br />
}</p>
<p>myFunc.call(w);</p>
<p>or just pass in an argument to the function:</p>
<p>function func2(w2){<br />
  w2.document.write(&#8220;bar&#8221;);<br />
}</p>
<p>func2(w);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derick Bailey</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2079</link>
		<dc:creator>Derick Bailey</dc:creator>
		<pubDate>Sun, 01 Jan 2012 00:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2079</guid>
		<description>in this version, you never override anything. you&#039;re creating an object with a &quot;write&quot; method, but never calling it.

you&#039;ve set the context of your immediate function by using the &#039;call&#039; method, but you&#039;re still using a closure (scope chain) up to the global function to find the &quot;document.write&quot; call from within that.

if you wanted to use your object instead of the global, you would need to call the version of &quot;write&quot; that is attached to the function&#039;s scope: &quot;this.document.write(...)&quot;.</description>
		<content:encoded><![CDATA[<p>in this version, you never override anything. you&#8217;re creating an object with a &#8220;write&#8221; method, but never calling it.</p>
<p>you&#8217;ve set the context of your immediate function by using the &#8216;call&#8217; method, but you&#8217;re still using a closure (scope chain) up to the global function to find the &#8220;document.write&#8221; call from within that.</p>
<p>if you wanted to use your object instead of the global, you would need to call the version of &#8220;write&#8221; that is attached to the function&#8217;s scope: &#8220;this.document.write(&#8230;)&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Sebring</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2078</link>
		<dc:creator>Jason Sebring</dc:creator>
		<pubDate>Sun, 01 Jan 2012 00:06:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2078</guid>
		<description>Revised version:

            (function(){
                var window = { // define a fake window object 
                    document : {
                        write : function(str) {
                          // fake write implementation
                          alert(str);
                        }
                    }
                },
                document = window.document;
                (function() { // wrap pre-existing script with this fakey closure
                    document.write(&#039;global&#039;);
                })();
            })();

Looks like you have to explicitly override the global vars, property by property. This would be cumbersome to fake the window object seems. Please advise on this one as I&#039;m interested on how to do that. John Resig? Douglas Crockford?</description>
		<content:encoded><![CDATA[<p>Revised version:</p>
<p>            (function(){<br />
                var window = { // define a fake window object<br />
                    document : {<br />
                        write : function(str) {<br />
                          // fake write implementation<br />
                          alert(str);<br />
                        }<br />
                    }<br />
                },<br />
                document = window.document;<br />
                (function() { // wrap pre-existing script with this fakey closure<br />
                    document.write(&#8216;global&#8217;);<br />
                })();<br />
            })();</p>
<p>Looks like you have to explicitly override the global vars, property by property. This would be cumbersome to fake the window object seems. Please advise on this one as I&#8217;m interested on how to do that. John Resig? Douglas Crockford?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Sebring</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2077</link>
		<dc:creator>Jason Sebring</dc:creator>
		<pubDate>Sat, 31 Dec 2011 23:52:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2077</guid>
		<description>Apparently NOT. I would think the same but WTF is this example I cooked up. It defies my understanding. Please help.
        

            var myFakeWindow = { // define a fake window object
                document : {
                    write : function(str) {
                      // fake write implementation
                      alert(str);
                    }
                }
            }; 
            (function(window) { // wrap pre-existing script with this fakey closure
                this.document.write(&#039;does alert&#039;);
                window.document.write(&#039;does alert&#039;);
                document.write(&#039;global&#039;);
            }).call(myFakeWindow,myFakeWindow);


That doesn&#039;t play by the rules of closures. Meaning &quot;window&quot; is set to be &quot;this&quot; and also have it explicitly overridden yet the &quot;document.write&quot; still calls the global window object. huh?</description>
		<content:encoded><![CDATA[<p>Apparently NOT. I would think the same but WTF is this example I cooked up. It defies my understanding. Please help.<br />
        </p>
<p>            var myFakeWindow = { // define a fake window object<br />
                document : {<br />
                    write : function(str) {<br />
                      // fake write implementation<br />
                      alert(str);<br />
                    }<br />
                }<br />
            };<br />
            (function(window) { // wrap pre-existing script with this fakey closure<br />
                this.document.write(&#8216;does alert&#8217;);<br />
                window.document.write(&#8216;does alert&#8217;);<br />
                document.write(&#8216;global&#8217;);<br />
            }).call(myFakeWindow,myFakeWindow);</p>
<p>That doesn&#8217;t play by the rules of closures. Meaning &#8220;window&#8221; is set to be &#8220;this&#8221; and also have it explicitly overridden yet the &#8220;document.write&#8221; still calls the global window object. huh?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: website hosting india</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2016</link>
		<dc:creator>website hosting india</dc:creator>
		<pubDate>Mon, 05 Dec 2011 12:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2016</guid>
		<description>I follow above the steps to implement global variable in java script.Most of the points are explained very clearly.</description>
		<content:encoded><![CDATA[<p>I follow above the steps to implement global variable in java script.Most of the points are explained very clearly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: developingchris</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2011</link>
		<dc:creator>developingchris</dc:creator>
		<pubDate>Sat, 03 Dec 2011 03:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2011</guid>
		<description>I find it interesting too, if you think of the dom as being all inside a self run function like this.

new function (){  var window = this;   // running before the html tag. and the closing brace   
&lt;html&gt;
&lt;/html&gt;
}


Then now you can see, there is no global scope, just everything as a closure scoped to window.</description>
		<content:encoded><![CDATA[<p>I find it interesting too, if you think of the dom as being all inside a self run function like this.</p>
<p>new function (){  var window = this;   // running before the html tag. and the closing brace   <br />
&lt;html&gt;<br />
&lt;/html&gt;<br />
}</p>
<p>Then now you can see, there is no global scope, just everything as a closure scoped to window.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derick Bailey</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2010</link>
		<dc:creator>Derick Bailey</dc:creator>
		<pubDate>Thu, 01 Dec 2011 18:02:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2010</guid>
		<description>this popped into my head a moment ago:

when a variable is accessed, whether or read or assignment, the runtime has to find the location of that variable. it first checks the local scope. if not found, it moved up to the next outer scope. if not found, it moves up again, until it finally reaches the global scope.

at any point in time, if the variable declaration is found in the scope that is currently being examined, the runtime uses that variable.

it would make sense that this same basic process works for both reads and writes, with one small exception:

when reading a variable, the runtime will reach the top of the global scope and if not found, it will throw an error saying the variable is not defined. 

but when assigning something to a variable, when the runtime reaches the very top of the global scope and doesn&#039;t find any declaration for the variable, the runtime shrugs and says &quot;whatever. i&#039;ll just create a variable for you, here&quot;.

so... it makes sense in my head, then, that you actually can create a variable in a scope outside of your current scope... but the only scope where this ever happens is the global scope, because the runtime just keeps searching up the scope stack until it either finds the declaration, or reaches the top.

... at least, that&#039;s how I would do it if i were building JavaScript. it&#039;s the simplest way to make this work, in my head. though I&#039;m sure there are many nuances, subtleties, and other cases that my simple line of thinking hasn&#039;t covered. :)</description>
		<content:encoded><![CDATA[<p>this popped into my head a moment ago:</p>
<p>when a variable is accessed, whether or read or assignment, the runtime has to find the location of that variable. it first checks the local scope. if not found, it moved up to the next outer scope. if not found, it moves up again, until it finally reaches the global scope.</p>
<p>at any point in time, if the variable declaration is found in the scope that is currently being examined, the runtime uses that variable.</p>
<p>it would make sense that this same basic process works for both reads and writes, with one small exception:</p>
<p>when reading a variable, the runtime will reach the top of the global scope and if not found, it will throw an error saying the variable is not defined. </p>
<p>but when assigning something to a variable, when the runtime reaches the very top of the global scope and doesn&#8217;t find any declaration for the variable, the runtime shrugs and says &#8220;whatever. i&#8217;ll just create a variable for you, here&#8221;.</p>
<p>so&#8230; it makes sense in my head, then, that you actually can create a variable in a scope outside of your current scope&#8230; but the only scope where this ever happens is the global scope, because the runtime just keeps searching up the scope stack until it either finds the declaration, or reaches the top.</p>
<p>&#8230; at least, that&#8217;s how I would do it if i were building JavaScript. it&#8217;s the simplest way to make this work, in my head. though I&#8217;m sure there are many nuances, subtleties, and other cases that my simple line of thinking hasn&#8217;t covered. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avi Block</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2009</link>
		<dc:creator>Avi Block</dc:creator>
		<pubDate>Thu, 01 Dec 2011 17:02:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2009</guid>
		<description>Really...

&quot;It looks like global scope is nothing more than the bi-product of the closure support that JavaScript has built into it, in combination with the outermost scope of the runtime (a DOMWindow or some other scope for CommonJS implementations).&quot;</description>
		<content:encoded><![CDATA[<p>Really&#8230;</p>
<p>&#8220;It looks like global scope is nothing more than the bi-product of the closure support that JavaScript has built into it, in combination with the outermost scope of the runtime (a DOMWindow or some other scope for CommonJS implementations).&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Mulligan</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2008</link>
		<dc:creator>Jason Mulligan</dc:creator>
		<pubDate>Thu, 01 Dec 2011 16:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2008</guid>
		<description>True, but the context of the copy appears to be a browser so it&#039;s a safe assumption.</description>
		<content:encoded><![CDATA[<p>True, but the context of the copy appears to be a browser so it&#8217;s a safe assumption.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joey Vano</title>
		<link>http://lostechies.com/derickbailey/2011/11/30/is-javascripts-global-scope-really-just-a-closure/#comment-2007</link>
		<dc:creator>Joey Vano</dc:creator>
		<pubDate>Thu, 01 Dec 2011 15:57:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/derickbailey/?p=688#comment-2007</guid>
		<description>In a browser environment, yes.</description>
		<content:encoded><![CDATA[<p>In a browser environment, yes.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
