<?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>ProtoAspx &#187; javascript</title>
	<atom:link href="http://www.gljakal.com/protoaspx/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gljakal.com/protoaspx</link>
	<description>Integrating Prototype and ASP.Net to create the next generation of web applications</description>
	<lastBuildDate>Thu, 26 Feb 2009 17:49:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google Chrome</title>
		<link>http://www.gljakal.com/protoaspx/2008/09/google-chrome/</link>
		<comments>http://www.gljakal.com/protoaspx/2008/09/google-chrome/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 17:55:16 +0000</pubDate>
		<dc:creator>Loris</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://gljakal.com/protoaspx/2008/09/google-chrome/</guid>
		<description><![CDATA[
Taking the web by surprise (didn&#8217;t they have a partnership with Mozilla?), Google has made another step towards a better web (or towards world domination, some may argue   ) by releasing its browser, Google Chrome, yesterday.
Based on the WebKit rendering engine (the one powering Konqueror and Safari), but otherwise designed from the ground-up, [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="google chrome logo" align="right" src="http://gljakal.com/protoaspx/wp-content/uploads/2008/09/chrome-128.png" width="128" height="128" /></p>
<p>Taking the web by surprise (didn&#8217;t they have a partnership with Mozilla?), Google has made another step towards a better web (or towards world domination, some may argue <img src='http://www.gljakal.com/protoaspx/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) by releasing its browser, <a href="http://www.google.com/chrome">Google Chrome</a>, yesterday.</p>
<p>Based on the WebKit rendering engine (the one powering Konqueror and Safari), but otherwise designed from the ground-up, Chrome promises web developers something they have being begging for since the (re)discovery of AJAX: <strong>blazing fast javascript performance </strong>and a <strong>multi-threaded</strong> browsing experience.</p>
<p>The performance gains come from the V8 javascript engine, that compiles javascript code down to <strong>bytecode, </strong>instead of just interpreting it.</p>
<p>Also, in Chrome, every tab is hosted in a separate (and sandboxed) process, which means that the javascript execution in one tab won&#8217;t impact negatively on the execution of other tabs.</p>
<p>You can read more on the online cartoon/book:</p>
<p><a title="Go to the google chrome online book" href="http://www.google.com/googlebooks/chrome/"><img alt="js_perf" src="http://gljakal.com/protoaspx/wp-content/uploads/2008/09/js-perf.png" width="500" height="225" /></a></p>
<h3>In the end, what does Chrome mean for us web developers?</h3>
<p>In my opinion, it means that soon we will have better browsers. </p>
<p>The release of Chrome comes after two very important announcements: <a href="http://weblogs.mozillazine.org/roadmap/archives/2008/08/tracemonkey_javascript_lightsp.html">TraceMonkey</a>, the new javascript engine that will power Firefox 3.1 and <a href="http://webkit.org/blog/189/announcing-squirrelfish/">SquirrelFish</a>, the new javascript engine that will power Safari 4.0, both of which promise tremendous peformance gains over SpiderMonkey (the engine powering Firefox 3.0, which should also be the fastest one at the moment).</p>
<p>The only browser left behind is, sadly, Internet Explorer, <a href="http://ejohn.org/blog/javascript-performance-rundown/">as shown by the benchmarks</a>. Sure, IE8 is much faster than IE7, but it&#8217;s still way slower than any of its competitors.</p>
<p>The browser war has just got a bit more intense and Microsoft will have to do much better if they want to remain competitive.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gljakal.com/protoaspx/2008/09/google-chrome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating classes with Prototype</title>
		<link>http://www.gljakal.com/protoaspx/2008/08/creating-classes-with-prototype/</link>
		<comments>http://www.gljakal.com/protoaspx/2008/08/creating-classes-with-prototype/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 09:01:43 +0000</pubDate>
		<dc:creator>Loris</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://gljakal.com/protoaspx/2008/08/creating-classes-with-prototype/</guid>
		<description><![CDATA[If you use Script.aculo.us or some other library written on top of Prototype, you probably have already encountered the Class object. Every time you call a new Effect.Appear() or a new Draggable() you are, in fact, instantiating a Prototype class.
Just like software developers abandoned procedural languages like C or Pascal for object oriented ones like [...]]]></description>
			<content:encoded><![CDATA[<p>If you use Script.aculo.us or some other library written on top of Prototype, you probably have already encountered the Class object. Every time you call a <code>new Effect.Appear()</code> or a <code>new Draggable()</code> you are, in fact, instantiating a Prototype class.</p>
<p>Just like software developers abandoned procedural languages like C or Pascal for object oriented ones like C++ all the way up to C# and Java, the same concept applies to the web: with (web)applications growing in complexity, it&#8217;s better to keep a modular approach and have a finite number of classes instead of a million of functions.</p>
<h3>A *very* basic example</h3>
<p>For this example, we are going to create a new class that observes an element and adds the &#8216;hover&#8217; class name to it when the mouse is over it. If you are impatient, you can see the end result <a href="http://gljakal.com/protoaspx/hover.html">on this demo page</a>.</p>
<p>Let&#8217;s start by declaring the new class. The class must be declared with the var keyword. The function <code>Class.create</code> takes a javascript object and transforms it into a Prototype class.</p>
<pre class="csharpcode"><span class="kwrd">var</span> HoverObserver = Class.create({
    initialize: <span class="kwrd">function</span>(element)
    {
    }
});</pre>
<p>The <code>initialize</code> function is very important, as it will be the constructor for our class.</p>
<p>Let&#8217;s add some more code:</p>
<pre class="csharpcode"><span class="kwrd">var</span> HoverObserver = Class.create({
    initialize: <span class="kwrd">function</span>(element)
    {
        <span class="kwrd">this</span>.element = element;
        element.observe(<span class="str">'mouseover'</span>, <span class="kwrd">this</span>.addClass.bindAsEventListener(<span class="kwrd">this</span>));
        element.observe(<span class="str">'mouseout'</span>, <span class="kwrd">this</span>.removeClass.bindAsEventListener(<span class="kwrd">this</span>));
    },
    addClass : <span class="kwrd">function</span>(<span class="kwrd">event</span>)
    {
        <span class="kwrd">this</span>.element.addClassName(<span class="str">'hover'</span>);
    },
    removeClass : <span class="kwrd">function</span>(<span class="kwrd">event</span>)
    {
        <span class="kwrd">this</span>.element.removeClassName(<span class="str">'hover'</span>);
    }
});</pre>
<p>Now that our class is actually doing something, we can start using it:</p>
<pre class="csharpcode">document.observe(<span class="str">'dom:loaded'</span>, <span class="kwrd">function</span>(){
    <span class="kwrd">new</span> HoverObserver($(<span class="str">'someDiv'</span>));
});</pre>
<p>You can see that the element we are passing to the constructor is actually passed by Prototype to the initialize function. </p>
<h3>Adding options</h3>
<p>Another nice thing we can do is to add an options parameter to the initialize function, so that we can pass one or more additional parameters to the constructor. For example, we can add an option to specify the class name to use instead of the default &#8216;hover&#8217;:</p>
<pre class="csharpcode"><span class="kwrd">var</span> HoverObserver = Class.create({
    initialize: <span class="kwrd">function</span>(element, options)
    {
        <span class="kwrd">this</span>.options = Object.extend({
            hoverClass : <span class="str">'hover'</span>
        }, options ||{});

        <span class="kwrd">this</span>.element = element;
        element.observe(<span class="str">'mouseover'</span>, <span class="kwrd">this</span>.addClass.bindAsEventListener(<span class="kwrd">this</span>));
        element.observe(<span class="str">'mouseout'</span>, <span class="kwrd">this</span>.removeClass.bindAsEventListener(<span class="kwrd">this</span>));
    },
    addClass : <span class="kwrd">function</span>(<span class="kwrd">event</span>)
    {
        <span class="kwrd">this</span>.element.addClassName(<span class="kwrd">this</span>.options.hoverClass);
    },
    removeClass : <span class="kwrd">function</span>(<span class="kwrd">event</span>)
    {
        <span class="kwrd">this</span>.element.removeClassName(<span class="kwrd">this</span>.options.hoverClass);
    }
});</pre>
<p>This is how we can use the options parameter:</p>
<pre class="csharpcode">document.observe(<span class="str">'dom:loaded'</span>, <span class="kwrd">function</span>(){
    <span class="kwrd">new</span> HoverObserver($(<span class="str">'someDiv'</span>), {hoverClass: <span class="str">'highlighted'</span>});
});</pre>
<p>The nice thing about this approach is not that we can specify every single parameter in the options argument, but that we don&#8217;t have to, since every parameter we don&#8217;t specify will use its default value. </p>
<h3>Common (and not-so-common) mistakes explained</h3>
<p><strong>Problem:</strong> I can&#8217;t use my object&#8217;s properties or methods, it says they are undefined!</p>
<p><strong>Solution:</strong> You forget to add a <code>.bind(this)</code> or <code>.bindAsEventListener(this)</code> to a function called by an event handler or an iterator.</p>
<pre class="csharpcode"><span class="rem">// wrong</span>
element.observe(<span class="str">'click'</span>, <span class="kwrd">this</span>.doClickAction);
elements.each(<span class="kwrd">this</span>.doSomething);
<span class="rem">// right</span>
element.observe(<span class="str">'click'</span>, <span class="kwrd">this</span>.doClickAction.bindAsEventListener(<span class="kwrd">this</span>));
elements.each(<span class="kwrd">this</span>.doSomething.bind(<span class="kwrd">this</span>));</pre>
<p><strong>Problem:</strong> Everything works fine in Firefox, Opera and Safari, but IE refuses to run my script. It looks like IE doesn&#8217;t even recognize it as JS code!</p>
<p><strong>Solution:</strong> You forgot a comma next to the last function declaration. </p>
<pre class="csharpcode"><span class="kwrd">var</span> MyClass = Class.create({
    initialize: function(){
    },
    method1: function(){
    },
    lastMethod: function(){
    },  <span class="rem">// &lt;-- this is the evil bastard</span>
});</pre>
<p>In this particular case none of the browsers I tested throws an error (they should!): IE simply refuses to parse the javascript file while the others ignore the syntactical error.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gljakal.com/protoaspx/2008/08/creating-classes-with-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
