<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Comment Feed for Channel 9 - JavaScript Reflection</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection/RSS"></atom:link>
	<image>
		<url>http://ecn.channel9.msdn.com/o9/c4f/images/4073227_100.jpg</url>
		<title>Channel 9 - JavaScript Reflection</title>
		<link></link>
	</image>
	<description>




I was writing an application for creating a manifest for JavaScript. I was unable to do it using DOM; instead I wrote my own application for parsing JavaScript files. It is little tough to peek into JavaScript objects, initial
 I could only get the members and fields using for-in. but that was not enough, what if someone stored a file locally and I have to write code to execute it, I would be able to get only the members, just executing members without passing the required input
 parameter if any will not work. The code below will parse the file and give me the options as to what input is required and if something is returned what it could possibly be returning except primitive types. Knowing these things in advance makes it easier
 to execute the code. Well it worked for me; let&#39;s see how it works for you. I know many people are looking for something like this.




Shreyans Kothari
My Space

Difficulty: Intermediate
Time Required: 
1-3 hours
Cost: Free
Software: 
Hardware: 
Download: Download








The code might not be perfect or might not do everything for you, since I wrote to cater my needs. But the basic utilities are implemented, you can add thing according to your needs. I am using the code from my project, therefore you will not see lot of
 other code that the project refers to, but you can input you JavaScript file to see the results or use the sample file that&#39;s provided in the project.

Some assumptions that I made; it will only look for &#39;prototype&#39; and &#39;this&#39; keywords in the JS code.
 
The core code: 
This code finds all the objects, their members and inputs for each member:
 

   1:        x = content.IndexOf(&amp;quot;.prototype.&amp;quot;, x);
   2:  &amp;nbsp;
   3:        for (int i = 0; x != -1; i&amp;#43;&amp;#43;)
   4:        {
   5:           int j = 0;
   6:           int k = 0;
   7:  &amp;nbsp;
   8:           for (j = x - 1; (&#39; &#39; != content[j] &amp;amp;&amp;amp; &#39;\n&#39; != content[j]); j--) { }
   9:           for (k = x &amp;#43; 10; (&#39; &#39; != content[k] &amp;amp;&amp;amp; &#39;=&#39; != content[k]); k&amp;#43;&amp;#43;) { }
  10:  &amp;nbsp;
  11:           int y = content.IndexOf(&amp;quot;function(&amp;quot;, k);
  12:  &amp;nbsp;
  13:           if (y == -1)
  14:              break;
  15:  &amp;nbsp;
  16:           int z = content.IndexOf(&#39;)&#39;, y);
  17:           string[] inputs = content.Substring(y &amp;#43; 8 &amp;#43; 1, z - (y &amp;#43; 8) - 1).Split(chArr);
  18:           string className = content.Substring(j &amp;#43; 1, x - j - 1);
  19:           string member = content.Substring(x &amp;#43; 10 &amp;#43; 1, k - (x &amp;#43; 10) - 1);
  20:  &amp;nbsp;
  21:           JSObject jsObj = null;
  22:  &amp;nbsp;
  23:           if (!jsObjs.TryGetValue(className, out jsObj))
  24:           {
  25:              jsObj = new JSObject(className);
  26:              jsObjs.Add(jsObj.Name, jsObj);
  27:           }
  28:  &amp;nbsp;
  29:           JSOperation operation = jsObj.addOperation(member);
  30:           
  31:           for (j = 0; j &amp;lt; inputs.Length; j&amp;#43;&amp;#43;)
  32:           {
  33:              if (inputs[j] != string.Empty)
  34:                 operation.addInput(inputs[j].Trim());
  35:           }
  36:           
  37:           x = content.IndexOf(&amp;quot;.prototype.&amp;quot;, x &amp;#43; 1);
  38:        }

 
 
Below code finds out all the methods that could be potential outputs for any of the operation, if the operation is not returning primitive data type then most likely it could be one of these types. The problem with associating the output to the operation
 is to find out what the return type is, since JavaScript supports loosely typed data, no one can be sure of the output data type unless the code is executed. therefore this code at least looks up all the non-primitive data types that could be one of the output
 types.  

   1:        x = content.IndexOf(&amp;quot;this.&amp;quot;, 0);
   2:  &amp;nbsp;
   3:        for (int i = 0; x != -1; i&amp;#43;&amp;#43;)
   4:        {
   5:            int j = 0;
   6:            int k = 0;
   7:            string strObj = string.Empty;
   8:  &amp;nbsp;
   9:            for (j = x - 1; &#39;{&#39; != content[j]; j--) { }
  10:            
  11:            for (k = j - 1; ; k--)
  12:            {
  13:                string fun = content.Substring(k, 8);
  14:                
  15:                if (fun == &amp;quot;function&amp;quot;)
  16:                    break;
  17:            }
  18:  &amp;nbsp;
  19:            int y = 0;
  20:  &amp;nbsp;
  21:            for (y = k &amp;#43; 8; content[y] != &#39;(&#39;; y&amp;#43;&amp;#43;) { }
  22:                  
  23:            if ((k &amp;#43; 8) &amp;lt; y)
  24:            {              
  25:                string objName = content.Substring(k &amp;#43; 9, y - (k &amp;#43; 9));
  26:                JSOutputFunction obj = new JSOutputFunction();
  27:                obj.ObjName = objName;
  28:                int cnt = 1;
  29:                      
  30:                for (k = j &amp;#43; 1; cnt != 0; k&amp;#43;&amp;#43;)                    
  31:                {
  32:                    if (content[k] == &#39;{&#39;)
  33:                        cnt&amp;#43;&amp;#43;;
  34:                          
  35:                    if (content[k] == &#39;}&#39;)
  36:                        cnt--;
  37:                }
  38:                      
  39:                strObj = content.Substring(j, k - j);
  40:                k = strObj.IndexOf(&amp;quot;this.&amp;quot;, 0);
  41:                      
  42:                for (; k != -1; )
  43:                {
  44:                    y = strObj.IndexOfAny(ch, k &amp;#43; 1);
  45:                    string fieldName = strObj.Substring(k &amp;#43; 5, y - k - 5);
  46:                          
  47:                    if (fieldName.Trim() != &amp;quot;toString&amp;quot;)
  48:                        obj.AddField(fieldName.Trim());                        
  49:                
  50:                    k = strObj.IndexOf(&amp;quot;this.&amp;quot;, k &amp;#43; 1);
  51:                }
  52:                outputUtil.AddOutput(obj);
  53:            }
  54:            else
  55:                j = x &amp;#43; 1;          
  56:            x = content.IndexOf(&amp;quot;this.&amp;quot;, j &amp;#43; strObj.Length);
  57:        }




After running the application, it would look something like this:  

 
I am currently working as a Program Manager at Microsoft.  
If I missed something or you have any questions, please email me. If you have any feedback or comments please do write I would definitely appreciate it.
 
 
Shreyans Kothari (MCAD)
Program Manager 
Aditi @ Microsoft 
Feedback/Comments
 
 

C4F Tags: 
Web 
Windows 
Utility , 
Web 
Windows 
Utility , 
Web 
Windows 
Utility 
</description>
	<link></link>
	<language>en</language>
	<pubDate>Sat, 18 May 2013 14:47:18 GMT</pubDate>
	<lastBuildDate>Sat, 18 May 2013 14:47:18 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<item>
		<title>Re: JavaScript Reflection</title>
		<description>
			<![CDATA[ <p>Take a look at this Javascript Serializer, similar idea:</p><p><a rel="nofollow" target="_new" href="http://www.iconico.com/workshop/jsSerializer/">http://www.iconico.com/workshop/jsSerializer/</a></p><p>posted by Nico Westerdale</p>]]>
		</description>
		<link>http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection#c633455568000000000</link>
		<pubDate>Mon, 05 May 2008 04:00:00 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection#c633455568000000000</guid>
		<dc:creator>Nico Westerdale</dc:creator>
	</item>
	<item>
		<title>Re: JavaScript Reflection</title>
		<description>
			<![CDATA[ <p>Hello Shreyans Kothari,</p><p>I was searching for javascript reflection with google and found your article. </p><p>I am not really shure if we both have the same understanding what javascript reflection is, but I found a very simple approach that resolves my javascript reflection problems... maybe you are interested in that <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-5.gif?v=c9' alt='Wink' /></p><p>In the web application I am currently creating, I needed to send an AJAX reply that invokes a javascript funktion which exists in the main html page. I thought that this can only be done via javascript reflection, means I am not directly calling a method, instead I send a simple string and that &quot;reflects&quot; the method call. In my application my php scripts now e.g. sends a &quot;@alert('generic reflection proxy')&quot; string. The javascript that requested the site just makes a call to &quot;setTimeout&quot; with the substring after the '@' symbol. </p><p>Kind Regards,</p><p>Markus</p><p>posted by Markus Bonkowski</p>]]>
		</description>
		<link>http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection#c633460752000000000</link>
		<pubDate>Sun, 11 May 2008 04:00:00 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection#c633460752000000000</guid>
		<dc:creator>Markus Bonkowski</dc:creator>
	</item>
	<item>
		<title>Re: JavaScript Reflection</title>
		<description>
			<![CDATA[ <p>&nbsp; &nbsp;Hello Shreyans Kothari,</p><p>I want actual coding of javascript with refleaction in a simple way</p><p>&nbsp; &nbsp;I am not really shure if we both have the same understanding what javascript reflection is, but I found a very simple approach that resolves my javascript reflection problems... maybe you are interested in that <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-5.gif?v=c9' alt='Wink' /></p><p>&nbsp; &nbsp;In the web application I am currently creating, I needed to send an AJAX reply that invokes a javascript funktion which exists in the main html page. I thought that this can only be done via javascript reflection, means I am not directly calling a method, instead I send a simple string and that &quot;reflects&quot; the method call. In my application my php scripts now e.g. sends a &quot;@alert('generic reflection proxy')&quot; string. The javascript that requested the site just makes a call to &quot;setTimeout&quot; with the substring after the '@' symbol.</p><p>&nbsp; &nbsp;Kind Regards,</p><p>&nbsp;Narender jakhar</p><p>posted by Narender Jakhar</p>]]>
		</description>
		<link>http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection#c633705300000000000</link>
		<pubDate>Wed, 18 Feb 2009 05:00:00 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection#c633705300000000000</guid>
		<dc:creator>Narender Jakhar</dc:creator>
	</item>
</channel>
</rss>