<?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: Ask Dan : Procedural VS object-oriented in javascript</title>
	<atom:link href="http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: DB</title>
		<link>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/comment-page-1/#comment-548</link>
		<dc:creator>DB</dc:creator>
		<pubDate>Fri, 11 Jul 2008 14:01:06 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=46#comment-548</guid>
		<description>One can use a procedural approach to invoke AJAX functionality without the use of global variables so I don&#039;t understand how use of globals is a telling point against the procedural approach. (See the example below).

After writing my library functions I can call them from any point in  my code, and reuse them without any conflicts that might arise from the use of globals:

InvokeAJAXRequest(&quot;dog&quot;,&quot;sound&quot;); 
InvokeAJAXRequest(&quot;cat&quot;,&quot;food&quot;); 

The code funnels through the InvokeAJAXRequest function which avoids the objection of no centralized interface.

Both the &quot;must use globals&quot; and &quot;no centralized place to make changes&quot; are straw man objections.  Bad code is bad code regardless of approach.

Moreover, I can accomplish the same tasks using considerably less keystrokes then the approach espoused elsewhere on this page.


function InvokeAJAXRequest(animal,responseType) 
{ 
var url = animal + responseType + &quot;.html &quot;
var xmlhttp = getHTTPRequest(); 

xmlhttp.open(&#039;GET&#039;, url, true); 
xmlhttp.onreadystatechange = function() { 
if(xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) 
{ 
     DoWhateverWithTheResponse(xmlhttp.responseText); 
} 
else 
{ 
    window.status = &quot;Loading...&quot;; 
} 
}; 
xmlhttp.send(null); 

} 
function getHTTPRequest() 
{ 
// native XMLHttpRequest (Firefox et al) 
if (window.XMLHttpRequest) 
{ 
    req = new XMLHttpRequest(); 
    return req; 
} 
//IE 6 
else if (window.ActiveXObject) 
{ 
    req = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;); 
    if (req) 
    { 
        return req; 
    } 
} 
} 

function DoWhateverWithTheResponse(text)
{
// Plug your returned HTML into the page
}</description>
		<content:encoded><![CDATA[<p>One can use a procedural approach to invoke AJAX functionality without the use of global variables so I don&#8217;t understand how use of globals is a telling point against the procedural approach. (See the example below).</p>
<p>After writing my library functions I can call them from any point in  my code, and reuse them without any conflicts that might arise from the use of globals:</p>
<p>InvokeAJAXRequest(&#8220;dog&#8221;,&#8221;sound&#8221;);<br />
InvokeAJAXRequest(&#8220;cat&#8221;,&#8221;food&#8221;); </p>
<p>The code funnels through the InvokeAJAXRequest function which avoids the objection of no centralized interface.</p>
<p>Both the &#8220;must use globals&#8221; and &#8220;no centralized place to make changes&#8221; are straw man objections.  Bad code is bad code regardless of approach.</p>
<p>Moreover, I can accomplish the same tasks using considerably less keystrokes then the approach espoused elsewhere on this page.</p>
<p>function InvokeAJAXRequest(animal,responseType)<br />
{<br />
var url = animal + responseType + &#8220;.html &#8221;<br />
var xmlhttp = getHTTPRequest(); </p>
<p>xmlhttp.open(&#8216;GET&#8217;, url, true);<br />
xmlhttp.onreadystatechange = function() {<br />
if(xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200)<br />
{<br />
     DoWhateverWithTheResponse(xmlhttp.responseText);<br />
}<br />
else<br />
{<br />
    window.status = &#8220;Loading&#8230;&#8221;;<br />
}<br />
};<br />
xmlhttp.send(null); </p>
<p>}<br />
function getHTTPRequest()<br />
{<br />
// native XMLHttpRequest (Firefox et al)<br />
if (window.XMLHttpRequest)<br />
{<br />
    req = new XMLHttpRequest();<br />
    return req;<br />
}<br />
//IE 6<br />
else if (window.ActiveXObject)<br />
{<br />
    req = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
    if (req)<br />
    {<br />
        return req;<br />
    }<br />
}<br />
} </p>
<p>function DoWhateverWithTheResponse(text)<br />
{<br />
// Plug your returned HTML into the page<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: -RE</title>
		<link>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/comment-page-1/#comment-305</link>
		<dc:creator>-RE</dc:creator>
		<pubDate>Fri, 22 Jun 2007 14:22:50 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=46#comment-305</guid>
		<description>It&#039;s like, what use are these fancy, high-level named variables when you can stuff your values into registers ? ;-)</description>
		<content:encoded><![CDATA[<p>It&#8217;s like, what use are these fancy, high-level named variables when you can stuff your values into registers ? <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/comment-page-1/#comment-304</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Fri, 08 Jun 2007 23:50:57 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=46#comment-304</guid>
		<description>Thanks BK for your comment... that&#039;s a great example.</description>
		<content:encoded><![CDATA[<p>Thanks BK for your comment&#8230; that&#8217;s a great example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/06/06/ask-dan-procedural-vs-object-oriented-in-javascript/comment-page-1/#comment-303</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Thu, 07 Jun 2007 12:27:42 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=46#comment-303</guid>
		<description>Other benefits could be the parameters and state store and also the possibility to move the object around.

The sample syntax you submitted in your question isn&#039;t quite descriptive and may prohibit to really understand the benfits. Say we rewrtie this as
new Ajax.Request(ServerName, PortName)
From there, you can then easily build new functions which receives your new object to easily communicate with the server. For instance, you could then have a script which would look like

var conn = new Ajax.Request(ServerName, PortName)
var cat = new Cat(conn)
cat.getSound()
cat.getFood()
var dog = new Doc(conn)
dog.getSound()
dog.getFood()

instead of
getCatSound(ServerName, PortName)
getDogSound(ServerName, PortName)
getCatFood(ServerName, PortName)
getDogFood(ServerName, PortName)

The second example could be a definite hassle to maintain. Also, the first example could easily manage things like connection pooling, performance stats, connection hacks, ... In the second method, it would be quite difficult since there is no centralized interface.

About the disposal of objects, except for COMs, do you have any documentation showing it is necessary to dispose of JS objects? Because since &quot;everything is object&quot; in JS (any function declaration creates a Function object), I don&#039;t think so or else, for every function we declare, we would have to dispose of them giving something like:

function test(){alert(&quot;test!&quot;)}


and don&#039;t use a declared function because you would then create another undisposed object... I don&#039;t think it could work like that...</description>
		<content:encoded><![CDATA[<p>Other benefits could be the parameters and state store and also the possibility to move the object around.</p>
<p>The sample syntax you submitted in your question isn&#8217;t quite descriptive and may prohibit to really understand the benfits. Say we rewrtie this as<br />
new Ajax.Request(ServerName, PortName)<br />
From there, you can then easily build new functions which receives your new object to easily communicate with the server. For instance, you could then have a script which would look like</p>
<p>var conn = new Ajax.Request(ServerName, PortName)<br />
var cat = new Cat(conn)<br />
cat.getSound()<br />
cat.getFood()<br />
var dog = new Doc(conn)<br />
dog.getSound()<br />
dog.getFood()</p>
<p>instead of<br />
getCatSound(ServerName, PortName)<br />
getDogSound(ServerName, PortName)<br />
getCatFood(ServerName, PortName)<br />
getDogFood(ServerName, PortName)</p>
<p>The second example could be a definite hassle to maintain. Also, the first example could easily manage things like connection pooling, performance stats, connection hacks, &#8230; In the second method, it would be quite difficult since there is no centralized interface.</p>
<p>About the disposal of objects, except for COMs, do you have any documentation showing it is necessary to dispose of JS objects? Because since &#8220;everything is object&#8221; in JS (any function declaration creates a Function object), I don&#8217;t think so or else, for every function we declare, we would have to dispose of them giving something like:</p>
<p>function test(){alert(&#8220;test!&#8221;)}</p>
<p>and don&#8217;t use a declared function because you would then create another undisposed object&#8230; I don&#8217;t think it could work like that&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
