<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wide thoughts &#187; properties</title>
	<atom:link href="http://gasper.kozak.si/blog/tag/properties/feed/" rel="self" type="application/rss+xml" />
	<link>http://gasper.kozak.si/blog</link>
	<description>a blog of one of those ... software developer geezers</description>
	<lastBuildDate>Sun, 22 Jan 2012 00:03:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Iterating over properties in window object</title>
		<link>http://gasper.kozak.si/blog/2009/09/06/iterating-over-properties-in-window-object/</link>
		<comments>http://gasper.kozak.si/blog/2009/09/06/iterating-over-properties-in-window-object/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 12:20:03 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[properties]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=436</guid>
		<description><![CDATA[I've been playing around a bit with JavaScript, and found out an interesting thing concerning property iteration in the <em>window</em> object in IE. In some cases, properties that are set on the window object, don't show up in the iteration.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around a bit with JavaScript, and found out an interesting thing concerning property iteration in the <em>window</em> object in IE. In some cases, properties that are set on the <em>window</em> object don&#8217;t show up in the iteration.<br />
<span id="more-436"></span><br />
As you probably know, setting a variable outside a function (in the so called global scope) actually writes to the window object.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">window.<span class="me1">X</span> = <span class="nu0">123</span>;</div>
</li>
<li class="li1">
<div class="de1">window<span class="br0">&#91;</span><span class="st0">&#39;X&#39;</span><span class="br0">&#93;</span> = <span class="nu0">123</span>;</div>
</li>
<li class="li1">
<div class="de1">X = <span class="nu0">123</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> X = <span class="nu0">123</span>;</div>
</li>
</ol>
</div>
<p>All of these do exactly the same: set a property <em>X</em> on object <em>window</em>.</p>
<p>But, when it comes to iterating window properties in IE (surprise), there seems to be a difference between the first two assignments and the last two. The code for iterating is simple; it iterates through all of the window&#8217;s properties and alerts if <em>X</em> is found.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> v <span class="kw1">in</span> window<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>v == <span class="st0">&#39;X&#39;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>v + <span class="st0">&#39;=&#39;</span> + window<span class="br0">&#91;</span>v<span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>With the first two assignments (the ones that write explicitly to <em>window</em>), the property shows up, but with the last two (where properties are implicitly assigned to <em>window</em>), it doesn&#8217;t. It works in Firefox, Opera, Chrome, Safari, but not in IE (not even in IE8). You can try it out <a href='http://kozak.si/widethoughts/wp-content/uploads/2009/09/obj_iter.html'>here</a>.</p>
<p>Why this is so, I can only guess. The property obviously exists, because <em>window['X']</em> and <em>window.X</em> always contain the correct value. One possibility is that iterating over object properties in IE doesn&#8217;t iterate over the properties that object actually <em>owns</em>, but rather over a separate collection of property names. This separate collection isn&#8217;t updated properly when variables are assigned to the <em>window</em> object implicitly. Another possibility is that such variables exist in a global closure, and as such aren&#8217;t true members of the <em>window</em> object. When you access <em>window.X</em> and if it doesn&#8217;t exist, it&#8217;s looked up in that closure.</p>
<p>Interesting if not life-changing stuff. Maybe I&#8217;ll do some more research on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://gasper.kozak.si/blog/2009/09/06/iterating-over-properties-in-window-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

