Archive for December, 2010

Javascript Gotchas : for vs for…in

Since the day I first acquainted myself with javascript, I thought ‘for’ and ‘’ served pretty much the same purpose. I always considered the latter to be merely of syntactic ease to a developer. Today I found out an important difference between these two constructs.

Suppose we run this code snippet:

var array = [1,2,3,4,5];
array.capacity = 10;

var str = '';
for(var i in array)
  str += array[i];

var str2 = '';
for(i=0; i < array.length; i++)
  str2 += array[i];

So by the end of the code execution, what are the values of str and str2? Before executing this code I always thought both will give the same result but the fact is :
str2 = ‘12345’ while str = ‘1234510’

If the highlighted line(No.2) is removed, both str and str2 give the same value ‘12345’.

If you closely read the description of the for…in consctruct from W3Schools, you’ll find the reason behind this difference in behavior. It says

“The for…in statement loops through the elements of an array or through the properties of an object.”

What entices us to believe that both ‘for’ and ‘’ are alike is the way these have been demonstrated. Almost all the top search result from google, give the code of iterating array elements while explaining the ‘for…in’ construct, so we never use it for accessing properties. Although, we usually do not keep properties in array objects but this behavior results when we get an array object from an external javascript library. Here is the example code for jQuery that reveals this difference:

var a = '[{"a":1},{"a":2},{"a":3}]';
var array = $.parseJSON(a);
for(var i in array)
  //process array[i]

Useful knowledge !


, , ,


ASP.Net : Construct a test Soap Request using sample

I recently did a project involving ASP.Net web services. I created one of my own and had to call it from some other part of the same project. While  debugging there were numerous occasions when I needed to build small Soap requests to test out functionality or even connectivity. It was annoying to write the Soap packets manually and it was extra-annoying when I could simply get a dummy Soap request from the web service page provided by ASP.NET(screenshot below).

Suppose, I have a simple (VB.Net) method in my web service that I want to call from somewhere:

<WebMethod()> _
Public Function HelloWorld(ByVal str As String) As String
    Return str
End Function

When I run the web service and click the "HelloWorld" link I get the following yellow looking page, which is not new to ASP.Net developers :

This page contains the sample SOAP request/response to call the web method. To use this effectively, I created a little HTML page with some javascript code that takes the sample request text as argument and returns a code that assigns this to a string. Here is the code:

function generateCode()
   var a = document.getElementById('txtSoap');
   var count = 0;
   var output = 'var str = \'\';\rstr += \'';
   for(var i=0;i<a.value.length;i++)
     if(a.value.charAt(i)=='\r' || a.value.charAt(i)=='\n')
     output += '\';';
     output += a.value.charAt(i);
     if(a.value.charAt(i)=='\r' || a.value.charAt(i)=='\n')
     output += 'str += \'';
   output += '\';';
   txtOutput.value = output;

Upon giving this input:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">
  <HelloWorld xmlns="">

We get this output:

var str = '';
str += '<?xml version="1.0" encoding="utf-8"?>';
str += '<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">';
str += '<soap:Body>';
str += '  <HelloWorld xmlns="">';
str += '    <str>string</str>';
str += '  </HelloWorld>';
str += '</soap:Body>';
str += '</soap:Envelope>';
str += '';

Useful. Isn’t it? I ended up using it 3 or 4 times the same day.

, , , ,

Leave a comment