<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jaiprakash : JScript</title><link>http://blogs.msdn.com/jaiprakash/archive/tags/JScript/default.aspx</link><description>Tags: JScript</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>For In Loop in JScript</title><link>http://blogs.msdn.com/jaiprakash/archive/2006/12/28/for-in-loop-in-jscript.aspx</link><pubDate>Thu, 28 Dec 2006 09:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1373255</guid><dc:creator>Jaiprakash</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaiprakash/comments/1373255.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaiprakash/commentrss.aspx?PostID=1373255</wfw:commentRss><description>&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Hi,&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;It's been more than one year since my last post. I will try to be regular now on :). &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;BTW, during this period, I have moved to Jscript team and will be sharing my little knowledge on&amp;nbsp;JScript with you all. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Recently I investigated a bug wherein user complained that for... In loop in JScript doesn't work. It doesn't enumerate through members of an array like the for..each loop of VBScript or C#. Since I am also new to Jscript and was not sure of the for...in behavior I decided to debug the code and here are the results...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;JScript For in loop is different from for-each of VBScript or C#. For each loop of VBScript and C# indeed iterates through the members of an object but Jscript for in loop actually iterates through indexes. What does that mean? Iterating through indexes not members? Well, let's work out an example...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;var arr = new Array(10, 20, 30);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (oBinding in arr)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;WScript.Echo(oBinding)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;What do you think should be the output of above code snippet? Shouldn't it be 10, 20, 30? &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;If you run above code, then output will be 0, 1, 2, which are actually array indices. If you want to print the members then you need to change above code as following...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;var arr = new Array(10, 20, 30);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (oBinding in arr)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;WScript.Echo(&lt;STRONG&gt;&lt;SPAN style="FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;arr[&lt;/SPAN&gt;&lt;/STRONG&gt;oBinding&lt;STRONG&gt;&lt;SPAN style="FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;]&lt;/SPAN&gt;&lt;/STRONG&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Above code snippet prints what you were expecting, 10, 20, 30. Hmmmm.... then why do i need a for..in loop at all.&amp;nbsp;I can just use a for loop to print the array members like following....&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (count = 0; count &amp;lt; arr.length; count++)&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;WScript.Echo(arr[count]);&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Correct! But the need of for..in loop arises when you have a sparse array.&amp;nbsp;JScript arrays are sparse arrays, that is, although you can allocate an array with many elements, only the elements that actually contain data exist. This reduces the amount of memory used by the array. Consider following example...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;var arr = new Array();&lt;BR&gt;arr[10] = 2;&lt;BR&gt;arr[15] = 4;&lt;BR&gt;arr[20] = 5;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (oBinding in arr)&lt;BR&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WScript.Echo(arr[oBinding])&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;}&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;In&amp;nbsp;above code array arr has only 3 valid elements though it's size is 20. If you use a for loop to access this array, you will be accessing all 20 elements which is unnecessay waste of CPU time. Instead if you use for...in loop you will just access 3 elements. Huge saving of CPU!!!&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;In other words what you can say is that for..in allows you to randomly access the array. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Same holds true for object properties also. What if you have a Jscript object but you don't know what all properties it holds. Answer is the for…in loop. Just use a for..in loop on that object and get hold of all the properties. Simple!&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1373255" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaiprakash/archive/tags/for+in+loop/default.aspx">for in loop</category><category domain="http://blogs.msdn.com/jaiprakash/archive/tags/JScript/default.aspx">JScript</category></item><item><title>Here I come...</title><link>http://blogs.msdn.com/jaiprakash/archive/2005/09/29/here-i-come.aspx</link><pubDate>Thu, 29 Sep 2005 17:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:475305</guid><dc:creator>Jaiprakash</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaiprakash/comments/475305.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaiprakash/commentrss.aspx?PostID=475305</wfw:commentRss><description>&lt;P&gt;Hi Friends,&lt;BR&gt;This is Jaiprakash, working as SDET in Visual J# team for the last few years. During this stint with J#&amp;nbsp; I have learnt quite a lot interesting things about different aspects of product development, about .net, about J# and had some real cool experiences. Through this channel I am going to share them with you all. &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;I would love to start with something which really confused me when I started working with J# team. In J# access specifier "&lt;B&gt;protected&lt;/B&gt;" has special behavior from other .net languages e.g. C#.&lt;BR&gt;If you write a &lt;STRONG&gt;protected&lt;/STRONG&gt; inner class in J#, compile it and refer the assembly in a C# project then your&amp;nbsp;J# class is shown &lt;STRONG&gt;public&lt;/STRONG&gt; not &lt;STRONG&gt;protected&lt;/STRONG&gt;. Confusing!!!&lt;/P&gt;
&lt;P&gt;So here is the explanation for this special behavior...&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;In J# following are the mappings of Access Specifiers to MSIL (Microsoft Intermediate Language - The intermediate language to which your code is compiled in .net) semantics…&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;Private in J#&amp;nbsp; -&amp;gt; &amp;nbsp;Private in IL&lt;BR&gt;Public in J# &amp;nbsp;-&amp;gt; &amp;nbsp;Public in IL.&lt;BR&gt;Package in J#&amp;nbsp;&amp;nbsp; &amp;nbsp;-&amp;gt;&amp;nbsp; &amp;nbsp;Nothing in IL&lt;BR&gt;Protected in J# &amp;nbsp;-&amp;gt; &amp;nbsp;Public in IL (By default)&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;The reason why J# &lt;STRONG&gt;protected&lt;/STRONG&gt; doesn’t map to protected in IL is that each access specifier is superset of the other and ‘&lt;STRONG&gt;protected&lt;/STRONG&gt;’ is superset of ‘&lt;STRONG&gt;package&lt;/STRONG&gt;’ in J#. In J# &lt;STRONG&gt;package&lt;/STRONG&gt; specifier means that access is available to classes in the same package and the package can be distributed in more than one assembly. Since ‘Protected’ in J# implies Package also, therefore in addition to obeying the general semantics it must confirm to Package semantics too. Since Package can be distributed across different assemblies, we simulate the required behavior by mapping ‘&lt;STRONG&gt;protected&lt;/STRONG&gt;’ in the source code to ‘&lt;STRONG&gt;public&lt;/STRONG&gt;’ in IL.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;This is the default behavior in J#. If you want to turn off this behavior then you must compile your source code with securescoping compiler switch (/securescoping[+|-]). This option marks "package" scope as "&lt;STRONG&gt;internal&lt;/STRONG&gt;" in meta data.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;If you are compiling in Visual Studio then you can turn on securescoping option from Project Properties pages. Following are the steps to follow...&lt;BR&gt;1. Right click on Project node in solution explorer. &lt;BR&gt;2. Click project properties. &lt;BR&gt;3. Click Build Tab in Project properties designer. &lt;BR&gt;4. Click Advanced Button. &lt;BR&gt;5. Check Secure Scoping check box. &lt;BR&gt;6. Build your project.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;The default behavior is there for the compatibility with Java language and secure scoping option is there to allow user to have consistency with other languages supported by CLR.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;Hmmm…it's getting too lengthy so let’s have a break. Keep visiting as I would be reveling more such confusing but interesting :) stories here.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=475305" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaiprakash/archive/tags/JScript/default.aspx">JScript</category></item></channel></rss>