<?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>Club AJAX – Dallas Ft. Worth Area AJAX Users Group covering: AJAX, JavaScript, HTML, CSS, and Design</title>
	<atom:link href="http://clubajax.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://clubajax.org</link>
	<description>Dallas Ft. Worth Area AJAX Users Group covering: AJAX, JavaScript, HTML, CSS, and Design</description>
	<lastBuildDate>Thu, 05 Jan 2012 14:17:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>JavaScript Context, Call and Bind &#8211; Ninja Level</title>
		<link>http://clubajax.org/javascript-context-call-and-bind-ninja-level/</link>
		<comments>http://clubajax.org/javascript-context-call-and-bind-ninja-level/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 14:59:46 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[original content]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[Object Oriented]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1823</guid>
		<description><![CDATA[In my previous article I showed the differences between scope and context, basic problems that arise and how to fix them. If you are just using some JavaScript and maybe jQuery, an understanding of scope is all that is needed to get you by. Once you start using objects or namespaces however, you&#8217;ll start to [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a title="JavaScript Scope and Context – Not the Same Thing!" href="http://clubajax.org/javascript-scope-and-context-not-the-same-thing/">previous article</a> I showed the differences between scope and context, basic problems that arise and how to fix them. If you are just using some JavaScript and maybe jQuery, an understanding of scope is all that is needed to get you by. Once you start using objects or namespaces however, you&#8217;ll start to run into issues with context and will need to use the keyword <code>this</code>. But when you get into object oriented JavaScript, you&#8217;ll need an advanced understanding of context and how to make it work for you. To do this, we&#8217;ll use the <code>call()</code> and <code>apply()</code> methods, and then a backwards compatible version of the new feature Mozilla recently released in JavaScript 1.8.5 called <code><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind">bind()</a></code>.<br />
<img class="aligncenter size-full wp-image-1867" title="JavaScript Function Context Bind" src="http://clubajax.org/wp-content/uploads/2011/12/FunctionContextBind.jpg" alt="JavaScript Function Context Bind" width="730" height="142" /><br />
<span id="more-1823"></span></p>
<h3 title="wtf">Who the Hell Would Ever Use this F@#ing Language Anyway?</h3>
<p>The title of this section is dedicated to a one-time Club AJAX attendee who looked at the next example and was so frustrated at how little he understood it, he started blurting out obscenities. Needless to say, he is now sentenced to life, doing Java.</p>
<p>So, to paraphrase Jack Nicholson&#8230; wait until you get a load of this:</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=AdvancedBrokenContext.js"></script><br />
<figcaption> Example #1 &#8211; Advanced Broken Context<br />
 </figcaption>
</figure>
<p><em>&#8220;Waitaminute!&#8221;</em> I hear you say. <em>&#8220;I applied the context to the function, and <code>onTimeout</code> fired — why didn&#8217;t <code>this.x</code> resolve? And who the hell would ever use this language, anyway?&#8221;</em></p>
<p>I empathize, really. But remember, you don&#8217;t know how to paint without knowing about the color red, and you can&#8217;t really say you know JavaScript unless you know about context and all of its pitfalls. When you are able to understand this concept, it will elevate you from just a guy who writes some JavaScript to outright NINJA!</p>
<p>Before we can decipher why the previous example doesn&#8217;t work, let&#8217;s change it up so we can better break down the issues.</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=AdvancedBrokenContextVariation.js"></script><br />
<figcaption> Example #2 &#8211; Advanced Broken Context Variation<br />
</figcaption>
</figure>
<p>Example #2 basically substitutes <code>otherMethod</code> for <code>setTimeout</code>, so we can side step that particular nuance for now. This example shows a similar situation: we are passing a method to another method and trying to invoke it. However, <code>this.x</code> is still undefined. The reason for this is kind of tricky.</p>
<p>Yes, we used <code>this</code> with <code>onTimeout</code>. But we only used <code>this</code> in order to <em>find</em> <code>onTimeout</code>, we didn&#8217;t use it to <em>invoke</em> <code>onTimeout</code>. If we didn&#8217;t use <code>this</code>, we would get an error saying the function <code>onTimeout</code> is not found, which makes sense, because that would indicate we want a function (as in, <code>var onTimeout();</code>), but what we really want is a method. So we use <code>this</code>, to get the method, and pass it to <code>this.otherMethod</code>.</p>
<div class="example">Refresher: Methods are associated to objects like: <code>object.myMethod = function(){}</code>; while functions are not: <code>var myFunc = function(){}</code>.</div>
<h3 title="theReason">The Reason</h3>
<p>Ironically, if <code>m()</code> or <code>otherMethod()</code> invoked <code>this.onTimeout</code>, it would work. But when we retrieved the method we sort of &#8220;extracted it&#8221; from the object, and passed it as a <em><strong>variable</strong></em>, of a <em><strong>function</strong></em> if you will. It&#8217;s no longer a <em>method</em>, it&#8217;s just a function with no call object bound to it. So we need to re-bind <code>this</code>, which we can do with the <code>call()</code> method:</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=AdvancedBrokenContextVariationFixed.js"></script><br />
<figcaption> Example #3 &#8211; Advanced Broken Context Variation Fixed<br />
 </figcaption>
</figure>
<div class="example">NOTE: The ECMAScript documentation refers to a method&#8217;s bound object as its <em>activation</em> object. David Flanagan, author of <a href="http://www.davidflanagan.com/javascript5/">JavaScript: The Definitive Guide</a> refers to it as a <em>call</em> object, which I like, because you can assign context with a function&#8217;s <code>call</code> method.</div>
<h3 title="isBroken">Is &#8220;this&#8221; Broken?</h3>
<p>We fixed it, but I sense dissatisfaction in you, young Skywalker. <em>&#8220;Why should I jump through these hoops? This language is broken!&#8221;</em> I agree that this aspect is at the least confusing and seems just plain wrong. But there is a reason for this behavior, believe it or not. Remember, JavaScript functions are just data and can be passed around as such.</p>
<p>Okay, if you don&#8217;t believe me, perhaps you&#8217;ll believe Brendan Eich from <em><a href="http://www.aminutewithbrendan.com/pages/20110303">this Talk</a></em>:</p>
<blockquote><p>
[The reason for the keyword "this"] is I wanted functions, which are the main idea in JavaScript, to also be methods; and for them to be methods, they had to have a receiving object who&#8217;s property was the value of that function.
</p></blockquote>
<p>That explanation was a bit&#8230; recursive. Perhaps a simple exercise will help. In Example #4, one object copies a method from another. What should be logged, 10 or 20?</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=CopiedMethod.js"></script><br />
<figcaption> Example #4 &#8211; Copied Method<br />
</figcaption>
</figure>
<p>If you answered 20, you&#8217;d be correct. Even though <code>o.m</code> was copied as data, it was still invoked on the last line with the <code>o2</code> object, which set its context. But you may see what the issue would be if a copied method carried its context around. Then the answer would be 10, derived from the original object. That would make JavaScript look even more broken.</p>
<p>The following is another example of the use of <code>call()</code>. You can borrow a method from one object and apply it to another object temporarily:</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=BorrowedMethod.js"></script><br />
<figcaption> Example #5 &#8211; Borrowing a Method from another Object<br />
 </figcaption>
</figure>
<p>In Example #5, <code>o.changeProps(1);</code> invokes as normal, but the last line, <code>o.changeProps.call(<strong>o2</strong>, 1);</code> calls that same method, yet invokes it within a different object. Admittedly, this example is a bit contrived and is probably making you squirm from all the best practices it violates. But it does show the versatility of the language and exactly how <code>call</code> works.</p>
<div class="example">Note: The first argument of <code>call()</code> should be an activation object and all arguments after that will be passed to the method. <code>apply()</code> works just like <code>call()</code> except the second argument should be an array and is often used to pass the arguments object.</div>
<p>Now that we have a better understanding of context and how to fix it, we can readdress the problem in Example #1. However, we can&#8217;t use <code>call()</code> to fix the context, because that would have to happen within the <code>setTimeout</code> function and we can&#8217;t change native code. Well, we <em>could</em>, but that would be like trying to waterproof a screen with a toothpick. Because the same problem will arise again and again, in callbacks, connections, and myriad other places. We have a better way.</p>
<h3 title="bind">Bind</h3>
<p>The problem can be fixed with the new <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind">bind</a> method in JavaScript.1.8.5. And in spite of the complexity of the problem, the solution is really quite simple:</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=AdvancedBrokenContextFixed.js"></script><br />
<figcaption> Example #6 &#8211; Advanced Broken Context Fixed<br />
</figcaption>
</figure>
<p>Now that <code>this</code> is bound <code>this.setTimeout</code>, we can pass it around and it won&#8217;t lose its context. And that&#8217;s it! Fixed! Good night nurse!</p>
<p>What&#8217;s that? Does this work in <em><strong>what</strong></em> other browser? Hm, I promised some sort of ninja status, didn&#8217;t I? That would necessitate some sort of explanation.</p>
<h3 title="bindUnderHood">Bind Under the Hood</h3>
<p>First of all, no, this does not work in <em>that</em> browser, but we can home brew a utility method that does, which we will add it to the prototype object of the Function constructor. Ever added the <code>forEach</code> method to Arrays? We&#8217;re doing the same thing here except with the Function&#8217;s prototype.</p>
<p>What we need to do is take our existing function and the context passed, and create a newly bound function. We are not going to return the bound function directly because if we did, it would fire immediately, which we don&#8217;t want. We are passing the bound function as data to be invoked later. Therefore we need to return <em>another</em> function that has the bound function inside it.</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=SimpleBindPrototype.js"></script><br />
<figcaption> Example #7 &#8211; Simple Bind Prototype<br />
 </figcaption>
</figure>
<p>You&#8217;ll notice that the function is bound with <code>apply()</code> instead of <code>call()</code>. This is because our utility code has no idea how many arguments may be passed, and <code>apply()</code> handles a variable amount&#8230; of variables.</p>
<p>Include the <em>Simple Bind Prototype</em> early within your code, and Example #6 will work — in all browsers.</p>
<h3 title="nativeBad">But Extending Native Objects is Bad</h3>
<p>Oh, so you&#8217;re one of those purists who believes you shouldn&#8217;t mess with native objects eh? That&#8217;s okay, I&#8217;m concerned about that too. The tide is shifting a bit in the JavaScript library developers community that <a href="http://perfectionkills.com/page/2/">it&#8217;s okay to extend native objects</a> (not host objects) — as long as those extensions are to fix gaps in weaker browsers with standardized functionality. And of course if it&#8217;s your code, you can do whatever you want!</p>
<p>And&#8230; what you want is to <strong>not</strong> extend the native object? That&#8217;s actually a good thing here, because the wrapper-implementation is a little more clear in how it works as there isn&#8217;t the confusion of what &#8220;this&#8221; is, as in Example #7.</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=SimpleBind.js"></script><br />
<figcaption> Example #8 &#8211; Simple Bind<br />
</figcaption>
</figure>
<p>If using the wrapper implementation, you would change the code from <code>setTimeout(this.onTimeout.bind(this), 1);</code><br />
to:<br />
<code>setTimeout(bind(this.onTimeout, this), 1);</code></p>
<p>The bind method passes the argument object, which is quite handy on callbacks. The following example is a recreation of a callback mechanism, which demonstrates how variables are passed:</p>
<figure><script type="text/javascript" src="https://gist.github.com/1396395.js?file=PassVars.js"></script><br />
<figcaption> Example #9 &#8211; Passing Variables<br />
</figcaption>
</figure>
<h3 title="MozillaImplementation">The Mozilla Implementation</h3>
<p>You may have taken a peak at the <a title="Mozilla MDN - Bind" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind">Mozilla bind page</a>, which would be only natural since I linked to it. I didn&#8217;t recreate their functionality because it&#8217;s a bit complex. Our solution handles 95% of what you would ever do with <code>bind()</code>, while theirs handles new concepts like binding to instance constructors and Arrays. But feel free to experiment!</p>
<h3 title="Conclusion">Conclusion</h3>
<p>Congratulations. If you made it this far into the blog and haven&#8217;t reached for a bottle of aspirin, bottle of alcohol, or a noose, that means you&#8217;ve managed to absorb one of the hardest concepts in JavaScript. Context is the key differentiator between JavaScript and all the other major languages.</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/javascript-context-call-and-bind-ninja-level/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript Scope and Context &#8211; Not the Same Thing!</title>
		<link>http://clubajax.org/javascript-scope-and-context-not-the-same-thing/</link>
		<comments>http://clubajax.org/javascript-scope-and-context-not-the-same-thing/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 14:35:25 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[original content]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1808</guid>
		<description><![CDATA[In forums and other places I often see people incorrectly use the term scope when they should be using the term context. Scope applies to the variable and functional access of a function, whereas context is the property and method access of a function. Essentially, scope is function-based, and context is object-based. Scope The following [...]]]></description>
			<content:encoded><![CDATA[<p>In forums and other places I often see people incorrectly use the term scope when they should be using the term context. Scope applies to the variable and functional access of a function, whereas context is the property and method access of a function. Essentially, scope is function-based, and context is object-based.<br />
<img src="http://clubajax.org/wp-content/uploads/2011/11/HyperCube.jpg" alt="JavaScript Scope" title="JavaScript Scope" width="730" height="230" class="aligncenter size-full wp-image-1857" /><br />
<span id="more-1808"></span></p>
<h3 title="scope">Scope</h3>
<p>The following shows a very simple example of the use of scope. <code>funcA</code> has access to <code>var a</code>, and <code>funcB</code> has access to <code>var b</code> and <code>var a</code>:</p>
<figure>
<script src="https://gist.github.com/1396031.js?file=SimpleScope.js"></script></p>
<figcaption>
Example #1 &#8211; Simple Scope<br />
</figcaption>
</figure>
<p>The second log in Example #1 fails because a function captures its scope, and keeps it all to itself. Nobody else has access to <code>funcB</code>&#8216;s scope, not even <code>funcA</code>. However, when <code>funcA</code> captured its scope, it included <code>funcB</code> and therefore <code>funcB</code> has access to all of <code>funcA</code>&#8216;s goodies. </p>
<p>Confused? Try looking at scope as a box made with one-way mirrors. Nothing can see inside the box, but the contents can see out.</p>
<h3 title="Closures">Closures</h3>
<p>A closure is when you then pass funcB to another function somewhere. The scope of <code>funcB</code> is then captured, or, &#8220;enclosed&#8221; and passed along with it. An in-depth look at closures is beyond the &#8220;scope&#8221; (heh heh) of this article, but you can learn more than you ever wanted to know about them from <a href="http://jibbering.com/faq/notes/closures/">Jibbering</a>.</p>
<h3 title="Context">Context</h3>
<p>When learning the JavaScript language, scope gets all of the attention, but the concept that is most difficult to grasp is <em>context</em>. Not knowing what context is and how it works is like trying to learn to paint and not being aware of the color red. The term context can loosely be related to usage of the keyword &#8220;this&#8221;. Hopefully the following example is obvious:</p>
<figure>
<script src="https://gist.github.com/1396031.js?file=SimpleContext.js"></script><br />
<figcaption>
Example #2 &#8211; Simple Context<br />
</figcaption>
</figure>
<p>Both x&#8217;s are able to be accessed because <code>this.x</code> is a property within the <code>m()</code> method&#8217;s context and <code>var x</code> is a variable within its scope. They do not clobber each other.</p>
<p>That was simple enough, but things start to get tricky when functions are used within methods. Take the following modification to the example:</p>
<figure>
<script src="https://gist.github.com/1396031.js?file=BrokenContext.js"></script><br />
<figcaption>
Example #3 &#8211; Broken Context<br />
</figcaption>
</figure>
<div class="example">
Refresher: Methods are associated to objects like: <code>object.myMethod = function(){}</code>; while functions are not: <code>var myFunc = function(){}</code>. Properties are associated to objects like: <code>object.myProperty = 96</code>; while variables are not: <code>var myVariable = 96</code>.
</div>
<p>Example #3 logs <em>1, undefined</em>. What happened is that the <code>f()</code> function was treated as a method. In the previous example, <code>this</code> worked because <code>m()</code> was a method and had the context of <code>o</code>. Note the difference between writing <code>o.m = function()</code> and <code>var f = function()</code>: <code>m()</code> has a receiving object while <code>f()</code> has no such object.</p>
<p>One solution to work around the previous nuance is by accessing <code>x</code> through the global object. Instead of <code>this.x</code>, <code>o.x</code> could be used and that would work. </p>
<p>But if the solution is so simple, then what is the need for the keyword <code>this</code>? The reason is you don&#8217;t always know what object you&#8217;re in, especially with the use of object instances. </p>
<figure>
<script src="https://gist.github.com/1396031.js?file=BrokenContextInstance.js"></script><br />
<figcaption>
Example #4 &#8211; Broken Context Instance<br />
</figcaption>
</figure>
<p>Now <code>this.x</code> is part of an instance object, and there can be several, or even hundreds or thousands of object instances. So it becomes impossible or at least unrealistic to try and access the properties via the object name.  </p>
<p>So perhaps we should just make <code>f()</code> a method, and then it will have access to <code>this.x</code>?</p>
<figure>
<script src="https://gist.github.com/1396031.js?file=ContextBrokenScope.js"></script><br />
<figcaption>
Example #5 &#8211; Context with Broken Scope<br />
</figcaption>
</figure>
<p>Oops. That was definitely not the right thing if you need access to both <code>var x</code> and <code>this.x</code>.</p>
<h3 title="ContextFixScope">Using Context to fix Scope</h3>
<p>So how to fix&#8230; this&#8230; example? One possible solution is to create <code>f()</code> with <code>this</code> context, <em><strong>within</strong></em> the <code>m()</code> method:</p>
<figure>
<script src="https://gist.github.com/1396031.js?file=ContextFixScope.js"></script><br />
<figcaption>
Example #6 &#8211; Context Fixing Scope<br />
</figcaption>
</figure>
<p>Now calling <code>this.f()</code>, works pretty well because now not only does <code>f()</code> has the context of the <code>instance1</code>, but it was created within <code>m()</code>, and so it captures that scope, which includes <code>var x</code>.</p>
<h3 title="ScopeFixContext">Using Scope to Fix Context</h3>
<p>We&#8217;ve probably all run into the following scenario at one time or another. </p>
<figure>
<script src="https://gist.github.com/1396031.js?file=SetTimeoutBrokenContext.js"></script><br />
<figcaption>
Example #7 &#8211; setTimeout with Broken Context<br />
</figcaption>
</figure>
<p>When I first encountered this situation several years ago, I was perplexed for days. It&#8217;s a hard problem to resolve because you don&#8217;t really know what&#8217;s wrong and what keywords to use to search for solutions.</p>
<p>The problem is the context is broken. The way JavaScript currently works is if a function doesn&#8217;t have context, or is not a method associated with an object, it executes in the global space. That should help identify the issue: <code>setTimeout</code> contains an anonymous function that is not associated with <code>this</code>, therefore, <code>this.onTimeout</code> does not resolve correctly.</p>
<p>We can fix this, literally, by assigning it to a variable:</p>
<figure>
<script src="https://gist.github.com/1396031.js?file=ScopeFixContext.js"></script><br />
<figcaption>
Example #8 &#8211; Scope to Fix setTimeout Context<br />
</figcaption>
</figure>
<p><code>this</code> is nothing more than a keyword that is is used to refer to an object. In this case the object is <code>o</code>, and previously it was the <code>C</code> object instance. Since it is just an object, we can assign it to a variable. By convention, the most popular variable name is <code>self</code> since we are referring to our &#8220;self&#8221; in a sense. Now when <code>onTimeout</code> is invoked, it is done so with the proper context and the code works correctly.</p>
<h3 title="conclusion">Conclusion</h3>
<p>Knowing the difference between scope and context will not only help you understand the JavaScript language better, but you&#8217;ll be better able to communicate with other developers and ask the proper questions. Likewise, knowing the difference between methods and functions, and properties and variables is important to communicate exactly what it is you&#8217;re referring to. Remember, <code>var x</code> and <code>this.x</code> are not the same thing!</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/javascript-scope-and-context-not-the-same-thing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Ask the Client for the Answer</title>
		<link>http://clubajax.org/dont-ask-the-client-for-the-answer/</link>
		<comments>http://clubajax.org/dont-ask-the-client-for-the-answer/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 15:01:40 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[business]]></category>
		<category><![CDATA[Career]]></category>
		<category><![CDATA[original content]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1779</guid>
		<description><![CDATA[When reading the title of this article you may immediately think that it seems obvious. You&#8217;re the commissioned expert, why would you be asking the client how to do your job? That&#8217;s precisely the point; I&#8217;m giving you the answer and not the problem. When given a solution the natural tendency is to take the [...]]]></description>
			<content:encoded><![CDATA[<p>When reading the title of this article you may immediately think that it seems obvious. You&#8217;re the commissioned expert, why would you be asking the client how to do your job?  That&#8217;s precisely the point; I&#8217;m giving you the answer and not the problem. When given a solution the natural tendency is to take the easy way and simply&#8230; implement it. But what is the problem you&#8217;re addressing? And is that the <strong>right</strong> solution? Will it work? How would you even know? So maybe, it&#8217;s not so obvious. In fact I would argue that being provided the solution is much more common than being told the problem.<br />
<img src="http://clubajax.org/wp-content/uploads/2011/11/BailWater.jpg" alt="Bailing Water" title="Bailing Water" width="640" height="327" class="aligncenter size-full wp-image-1803" /><br />
<span id="more-1779"></span></p>
<p>Understanding this dynamic helps build better communication with the client. If the client asks that the button be moved to the left, and you respond it&#8217;s better on the right, you may insult them and their process. Their process may have been: </p>
<ul>
<li>Recommendations from usability studies. </li>
<li>Consulting with expert button location professionals</li>
<li>Several all night meetings among team members who sweated over the button location</li>
</ul>
<p>It&#8217;s likely none of these, and was probably that the client thought about it in his car on the way to work. But either way, the client had a process which incidentally left you out. How do you resolve this? Ask questions.</p>
<p>When presented with only a solution it becomes a riddle. This may put you in the position of having to reverse engineer their decision to get to the problem. <em>Why do you want the button there? Is it because users aren&#8217;t clicking on it? Did a customer complain? Is it because your daughter is left handed?</em></p>
<p>For the most part, moving a button is easy and is probably the best solution to make the client happy. But what if it&#8217;s a complex solution like asking you to increase the buffer time on the video player? If I don&#8217;t ask questions, it may go down like this:</p>
<p style="padding-left:30px;">
I spend a few hours increasing the buffer time from 2 seconds to 5 seconds (this took a long time because it auto-buffered before and there was no code to have a custom setting). The changes are tested and pushed to production. The client now asks for a 10 second buffer. The changes are pushed. The client then says this isn&#8217;t working, now customers are complaining that the video takes to long to load, and the buffer time isn&#8217;t even helping&#8230; on <strong>mobile phones</strong>. You reply, <em>&#8220;Oh, well this code doesn&#8217;t affect mobile phones.&#8221;</em> The customer replies <em>&#8220;Oh, well, I need the videos to load faster on mobile phones.&#8221;</em>
</p>
<p>Getting the problem at the end of the process is obviously unproductive. All those changes were made was a waste of time and money, and it turns out they hurt the product while the real problem wasn&#8217;t addressed &mdash; which in this case may not have something completely different, like a video encoding problem.</p>
<p>Unfortunately we developers are often not the ones interfacing with the client. When a project manager tells me he needs a change, I get real frustrated when he can&#8217;t answer my questions. <em>&#8220;But the client made the request, so I didn&#8217;t question it.&#8221; </em>The truth is: we are not in the business of doing as we are told. We are in the business of making the client successful. Which may at times even be in spite of the client&#8217;s wishes. I&#8217;m not saying to be defiant, because sometimes the client may need to go through a learning process himself. Just make sure you have your change order prepared.</p>
<h3>The Rookie Dilemma</h3>
<p>What do you do if you are so low on the developer food chain that you simply are not allowed in the process and have to just do what you are told? Same thing &mdash; ask questions. If you&#8217;re not asking questions you&#8217;re not learning. But note there is a fine line between asking questions and questioning authority. <em>&#8220;Moving the button to the left destroys the flow of the page &mdash; it looks stupid there!&#8221;</em> is more professionally phrased as <em>&#8220;Okay, although that would upset the flow of the page. Just so I know, why are we moving it?&#8221;</em> If your questions are intelligent enough, you may trigger a new discussion with the client &mdash; and you may even be invited to the party. We&#8217;ve all started low on the totem pole at one or multiple times in our career. Asking good questions is just one way of making the climb.</p>
<h3>Conclusion</h3>
<p>Don&#8217;t give the client what they ask for &mdash; give them what they need. I&#8217;m not going Zen on you. It&#8217;s a simple matter that most of the time they don&#8217;t really know what they need, so they ask for the wrong thing. The most successful man I ever worked for once said &#8220;It&#8217;s easy to make the smart clients look smart. But the real money is in making stupid clients look smart.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/dont-ask-the-client-for-the-answer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Web Applications with MDV</title>
		<link>http://clubajax.org/building-web-applications-with-mdv/</link>
		<comments>http://clubajax.org/building-web-applications-with-mdv/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 13:52:12 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Meeting]]></category>
		<category><![CDATA[original content]]></category>
		<category><![CDATA[Presentation]]></category>
		<category><![CDATA[MDV]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1772</guid>
		<description><![CDATA[MDV or &#8220;model-driven views&#8221;, eschews the concept of widgets in favor of template-based views which are behavior-based and update themselves when the data has changed. Max Motovilov gave a presentation on MDV which was enlightening and thought provoking. He showed how he leveraged The Dojo Toolkit to create behaviors and data stores which update his [...]]]></description>
			<content:encoded><![CDATA[<p>MDV or &#8220;model-driven views&#8221;, eschews the concept of widgets in favor of template-based views which are behavior-based and update themselves when the data has changed.</p>
<p>Max Motovilov gave a presentation on MDV which was enlightening and thought provoking. He showed how he leveraged The Dojo Toolkit to create behaviors and data stores which update his views through his custom-built template system.</p>
<p>Max&#8217;s detailed presentation can be <a href="http://clubajax.org/presentations/mdv-web-applications/">seen here</a>, and you can also see the <a href="https://github.com/MaxMotovilov/adstream-js-frameworks/tree/master/examples/blah">examples he used on GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/building-web-applications-with-mdv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Dart &#8211; Should JavaScript be Replaced?</title>
		<link>http://clubajax.org/google-dart-should-javascript-be-replaced/</link>
		<comments>http://clubajax.org/google-dart-should-javascript-be-replaced/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 13:55:22 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[Dart]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1744</guid>
		<description><![CDATA[A memo from a Google employee was leaked earlier this month exposing the new plan for Dart, which they claim to be a new programming language for structured web programming. The memo goes into some detail on what Dart would be, but doesn&#8217;t go into much detail on why Dart should be. In other words, [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://markmail.org/message/uro3jtoitlmq6x7t">memo from a Google employee</a> was leaked earlier this month exposing the new plan for Dart, which they claim to be <a href="http://gotocon.com/aarhus-2011/presentation/Opening%20Keynote:%20Dart,%20a%20new%20programming%20language%20for%20structured%20web%20programming">a new programming language for structured web programming</a>. The memo goes into some detail on what Dart would be, but doesn&#8217;t go into much detail on <strong>why</strong> Dart should be. In other words, it doesn&#8217;t explicitly state the deficiencies in JavaScript. Do they have a point? Should JavaScript be replaced?</p>
<p><img class="aligncenter size-full wp-image-1752" title="ReplaceJS" src="http://clubajax.org/wp-content/uploads/2011/09/ReplaceJS.jpg" alt="Dart to replace JavaScript?" width="730" height="348" /><br />
<span id="more-1744"></span></p>
<h3>Precedence</h3>
<p>You already can run other languages in a browser. There is a <a href="http://www.tcl.tk/man/tcl/tutorial/Tcl0a.html">TCL</a> plugin; IE has VB Script; and there is also C# in the form of Silverlight. And who can forget the most popular plugin and language on the web today&#8230;. <a href="http://download.oracle.com/javafx/">Java FX</a>!?</p>
<p>Flash is the obvious precedent, since at one time in the early 2000&#8242;s it actually did replace JavaScript. However, since then we&#8217;ve had HTML5 with new language APIs, a new and improved browser war with faster engines, and <a href="http://en.wikipedia.org/wiki/ECMAScript#ECMAScript_Harmony">Harmony</a> with buy-in from all the browser vendors.</p>
<p>Flash is also a good example of how you can reach too far. Flash has and has had many impressive features, but a lot of these were later hindered by internet security issues. So we can, for example, wish for a browser language with access to the local file system, but that&#8217;s not going to happen. So what then can we wish for?</p>
<h3>What is not the problem</h3>
<p>One thing I don&#8217;t want to see is strict typing. Okay, maybe a little bit of optionally typed variables, preferably in vectors or typed arrays for speeding up Canvas and 3D. Otherwise, loose typing is what makes JavaScript so versatile. AS3 went to a rigid, Java-like syntax, and it&#8217;s not very fun to code.</p>
<p>As we all know, JavaScript is not the problem nor bottleneck in web page performance. It&#8217;s the DOM, browser compatibility, and server requests. This narrows down the situation to pure language features. HTML5 has added features that make JavaScript more robust than ever. Workers are a simple API (though more difficult in practice) that allow multiple threads for unprecedented scripting power. They also added Storage, so now we don&#8217;t have to suffer at the hands of a 4K cookie. Once Web Sockets security is figured out, giving us faster and more consistent server connections, what more do we need?</p>
<h3>Structure</h3>
<p>I love the lack of structure and wild west nature of developing in JavaScript. But realistically, it&#8217;s very difficult for devs to jump between different libraries like jQuery, MooTools or Dojo. This is a delicate balancing act however, because JavaScript is already a great language and the task is to make it better, not more restrictive. Any structural code changes need to be optional.</p>
<h3>Modules</h3>
<p>It&#8217;s flabbergasting to me that CSS has <code>@import</code> and JavaScript doesn&#8217;t. I think that can only be explain with &#8220;What the Hell?&#8221; Dojo&#8217;s bread and butter is its script loader — solving a problem that shouldn&#8217;t be there in the first place.</p>
<h3>Packages</h3>
<p>The unique thing about a Flash movie SWF is that it is a self contained file that can include all the necessary resources, minimizing or eliminating multiple server requests. JavaScript has no such ability, save some real kludgy experiments. Mozilla is addressing this issue with their <a href="http://people.mozilla.com/~jlebar/respkg/">Resource Packages Proposal</a>.</p>
<h3>Classes</h3>
<p>Yes, JavaScript has prototypes and we have our <a href="http://clubajax.org/simple-and-easy-javascript-inheritance/">syntactic sugar to give us inheritance</a>, but again, it&#8217;s hard to write standardized code. jQuery, MooTools and Dojo all address this limitation is vastly different ways, fracturing web development.</p>
<h3>Fix this!</h3>
<p>It would be nice if &#8216;this&#8217; were optional. Object oriented code is a bit discouraging in JavaScript because you are punished with tens of hundreds of &#8216;this&#8217; keywords peppered throughout your code, which bloats it and doesn&#8217;t help readability. It also hampers newbie developers who have trouble grokking the concept of context invoked functions (like say, calling an object method from a <code>setTimeout</code>). Adobe claims to have fixed &#8216;this&#8217; in ActionScript 3.0&#8230; but not really. ActionScript is essentially Java with JavaScript mashed on top of it. When you write actual ECMAScript in an ActionScript file, you need to use &#8216;this&#8217; or the compiler gets confused.</p>
<p>What I&#8217;m suggesting would require the co-mingling of context and scope. The variable search would start with the initially scoped function, then the object and prototype chain, then the remainder of the scope chain. Would this implementation affect performance? When I write a JavaScript engine I&#8217;ll let you know! Until then, I&#8217;m punting to the browser vendors!</p>
<h3>Protected Variables</h3>
<p>Yes, everyone wants their precious private variables, but <a href="http://clubajax.org/javascript-private-variables-are-evil/">I&#8217;m on record for not being a fan</a>, as I feel they are over used and abused. Protected variables on the other hand, give you the best of both worlds: the public API can be exposed as the clear intent, and the protected variables and methods can only be accessed or changed if the class is extended to explicitly override them. If you want to change my library code you need to work for it, yo!</p>
<h3>Maff</h3>
<p>Yes, I&#8217;m referring to the fact that <code>.1 + .2 = 0.30000000000000004</code>. I understand that it&#8217;s <a href="http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html">common problem in floating point numbers using binary digits.</a> I also concede that this situation rarely is an issue for me, because I do largely graphical work, not work that requires extreme numerical accuracy like accounting. Regardless, a new Math namespace would be a welcome addition, and even better, it could be a strictly typed namespace that allows only floating point numbers.</p>
<h3>Tooling</h3>
<p>The Google memo says that one major problem with JavaScript is the lack of ability to &#8220;tool it&#8221;. Not being a server side programmer (and when I play one on TV, I use PHP), I&#8217;m not familiar with these tools, or lack thereof. But I&#8217;m guessing they are referring to the loose and dynamic nature of JavaScript not allowing an IDE like Eclipse to track objects and report broken code.  I&#8217;m going to punt a little bit here, and claim: the lack of tools forces you to write better, loosely coupled, encapsulated code. Tools make you lazy! I can hammer in nails with my forehead and drive screws with my teeth! Raaawarr!</p>
<h3>Standards Politics</h3>
<p>At first glance it may seem to be a great benefit to develop a language outside of the <a href="http://www.w3.org/">W3C</a> and their sometimes draconian politics, their self centered policies, and their ability to take a great idea and turn it into a shitty one. Lots of languages are written, but only a few catch on. For every great language like Python, you get several like DOS, LotusScript, Authorware, Lingo, or Cold Fusion — or any other Macromedia derived language. Speaking of Macromedia, the early Flash scripting language was proprietary, cumbersome, and unusable. They switched to an ECMAScript-based syntax, and ActionScript exploded with success.</p>
<p>Believe me, I&#8217;m not championing the standards body that almost killed off HTML. Nor am I saying that designing a language by committee is a good idea. It&#8217;s just that you are fighting an uphill battle if you throw out the standards and start from scratch, even if it&#8217;s a better idea. The <a href="http://en.wikipedia.org/wiki/United_States_customary_units">US System of Measurements</a> is an example of design by a committee of retarded orangutangs flinging their feces at finger paintings to make their discussions — yet the Metric System was not widely adopted. Yet while Python was invented by <a href="http://en.wikipedia.org/wiki/Guido_van_Rossum">one guy</a>, it evolved in an open source community.</p>
<h3>Conclusion</h3>
<p>Can JavaScript be replaced? My guess is at worst never, and at best ten plus years. JavaScript developers are very protective of their domain, sometimes to a fault, and they don&#8217;t appear to be very welcoming of changes; they often are even averse to Harmony proposals.  New technologies require a <a href="http://en.wikipedia.org/wiki/Killer_app">killer app</a> in order to catch on. Flash had <a href="http://youtube.com">YouTube</a>, Silverlight had <a href="http://netflix.com">NetFlix</a>, and Java FX had&#8230; well, I rest my case. Since Dart has corporate giant Google behind it, a killer app is definitely not out of the question. And if so, the big brains at Google could make this happen.</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/google-dart-should-javascript-be-replaced/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>What&#8217;s New in HTML5</title>
		<link>http://clubajax.org/whats-new-in-html5/</link>
		<comments>http://clubajax.org/whats-new-in-html5/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 16:07:28 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1723</guid>
		<description><![CDATA[If you live in the DFW area and you&#8217;re not coming to our meetings, you&#8217;re missing out! These aren&#8217;t &#8220;user groups&#8221; per se, but more like a get together where we cut up and have fun. Last night&#8217;s was an especially good time. Then we do the after-party at the bar! The Ajax News was [...]]]></description>
			<content:encoded><![CDATA[<p>If you live in the DFW area and you&#8217;re not coming to our meetings, you&#8217;re missing out! These aren&#8217;t &#8220;user groups&#8221; per se, but more like a get together where we cut up and have fun. Last night&#8217;s was an especially good time. Then we do the after-party at the bar!</p>
<p>The Ajax News was not recorded this time, but it was based on existing presentations from Google IO. The latest and greatest in HTML5 have landed in the Google-backed <em>Chrome,</em> which has surpassed the Apple-backed <em>Safari</em> as the browser introducing the most cutting edge features.  Safari, Firefox, IE, and Opera better put it in high gear if they want to keep up.</p>
<p><a href="http://html5-demos.appspot.com/static/html5-whats-new/template/index.html#1">http://html5-demos.appspot.com/static/html5-whats-new/template/index.html#1</a></p>
<p><a href="http://www.htmlfivewow.com/slide1">http://www.htmlfivewow.com/slide1</a></p>
<p>NOTE: These presentations must be run in Google Chrome 12+. Also, you may need to enable some features, like audio in the <em>about:flags</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/whats-new-in-html5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Video: Webpage Design Basics for Non-Designers</title>
		<link>http://clubajax.org/video-webpage-design-basics-for-non-designers/</link>
		<comments>http://clubajax.org/video-webpage-design-basics-for-non-designers/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 13:21:45 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[Graphical Design]]></category>
		<category><![CDATA[Meeting]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1711</guid>
		<description><![CDATA[Posted is the video and presentation of web design for non-designers. We cover a lot of basics, including logo design, layout, fonts, and color theory.]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-1715" title="Designing-Web-Pages" src="http://clubajax.org/wp-content/uploads/2011/07/Designing-Web-Pages.jpg" alt="Designing-Web-Pages" width="240" height="180" />Posted is the <a href="http://clubajax.org/videos/webpage-design-basics-for-non-designers/">video</a> and <a href="http://clubajax.org/presentations/webpage-design-basics-for-non-designers/">presentation</a> of web design for non-designers. We cover a lot of basics, including logo design, layout, fonts, and color theory.</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/video-webpage-design-basics-for-non-designers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple and Easy JavaScript Inheritance</title>
		<link>http://clubajax.org/simple-and-easy-javascript-inheritance/</link>
		<comments>http://clubajax.org/simple-and-easy-javascript-inheritance/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 15:43:12 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[original content]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Object Oriented]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1638</guid>
		<description><![CDATA[For object inheritance, I had been using the traditional method of assigning methods and properties to the function&#8217;s prototype. Because my project was relatively simple, I could get away with this for a while. But as the project grew in scope, using the prototype started proving unwieldy. It worked, but the code was getting messy [...]]]></description>
			<content:encoded><![CDATA[<p>For object inheritance, I had been using the traditional method of assigning methods and properties to the function&#8217;s prototype. Because my project was relatively simple, I could get away with this for a while. But as the project grew in scope, using the prototype started proving unwieldy. It worked, but the code was getting messy and hard to read. I decided it was time to implement an inheritance system. Not too complex, just something to help me organize my code. I ended up creating what I call my Declare Inheritance Helper.<br />
<img src="http://clubajax.org/wp-content/uploads/2011/06/inheritance.jpg" alt="inheritance" title="inheritance" width="730" height="131" class="aligncenter size-full wp-image-1664" /><br />
<span id="more-1638"></span><br />
Even though I am a big proponent of Dojo, I have my own personal library that I use for the BetterVideo video player project. I started rolling my own because it needed to be as small as possible, and any unneeded functionality meant extra bytes on our clients&#8217; pages.</p>
<p>Before getting into Declare, here is an example of the traditional prototype method:</p>
<pre><code>
var Play = function(options){
	this.type = "play";
	this.init(options);
	this.isPlaying = false;
	this.toggle = function(){
		// toggle between play and pause
	}
}

var Volume = function(options){
	this.type = "volume";
	this.init(options);
	this.openVolumeSlider = function(){
		// open popup
	}
}

var Base = {
	init: function(options){
		this.node = options.node;
		this.player = options.player;
		this.video = this.player.video;
	},
	update: function(p, vert){
		// do something common...
	}
};

Play.prototype = Base;
Volume.prototype = Base;
</code></pre>
<p>There are several problems with this. The first (minor) annoyance is that as I would add new components, I would forget to add the <code>Component.prototype = Base;</code> code. Next, in order to add properties and methods to the main classes (Play and Volume) they have to use <code>this</code> within the constructor, as opposed to my preferred style of assigning them within an object literal, as used in the Base class. There are minor technical penalties for assignments in the constructor, but mostly I don&#8217;t like having to use <code>this</code> before every line which makes the code less readable. But fundamentally, this is limiting. I can only combine a parent class and a child class, and that&#8217;s it. Any other combination overwrites something. </p>
<h3>The Declare Inheritance Helper</h3>
<p>I started with the <a href="http://trac.dojotoolkit.org/browser/dojox/trunk/drawing/util/oo.js"><em>declare</em> system I wrote for DojoX Drawing.</a> This system accepts functions as constructors and objects to be mixed in &mdash; or functions as classes where its methods and properties are already mixed in. This did what I wanted, but I realized that it was still on the ugly side. Jamming a function as an argument followed by an object literal followed by object names&#8230; it&#8217;s self contained but there was no flow. I decided to rework it. </p>
<p>I realized that I didn&#8217;t need to have a separate function and object which gets put together via a prototype &mdash; I could just pull the function out of the object! Duh! That&#8217;s what Dojo does!</p>
<p>So it was a simple matter of having a conventionally named method act as the constructor. First I used &#8220;init&#8221;:</p>
<pre><code>
var Foo = declare({
	init: function(){
		// this is my constructor
	},
	update: function(){
		// do something else!
	}
});
</code></pre>
<p>There was still one problem. I ran into a couple of situations where I wanted to know what class I was within. I started by just sticking a type in every class, but this was extra work, not to mention bytes. I originally wondered if I could somehow pull the name out of the assigned variable, like:</p>
<pre><code>
var Foo = declare({
	this.declaredClass = getInstanceVariableName(); // not a real method!
})
</code></pre>
<p>Well of course you can&#8217;t do that, but that thought process led to an idea. What I could do is name the constructor the same name as the class &mdash; and capitalize it &mdash; which is not only semantically correct (a class function should conventionally begin with a capital) but this can signal to my helper code which method is the constructor. And it&#8217;s not so crazy of an idea since this is the same convention used in AS3 and Java. So now my code might look something like this:</p>
<pre><code>
// sub classes
var Foo = {
	Foo: function(){
		// I am capitalized, therefore I am... constructor.
	},
	update: function(){
		// update something!
	}
};

var Bar = {
	Bar: function(){
		// Initialize, constructor!
	},
	draw: function(){
		// Draw something!
	}
};

var Zap = {
	Zap: function(){
		// By now you know what this is!
	},
	remove: function(){
		// remove something!
	}
};

// main, derived class
var CustomClass = declare(Foo, Bar, Zap);

// instance

var myClass = new CustomClass();
</code></pre>
<h3>Declare Library Code Explained</h3>
<p>The following is the declare inheritance helper. It&#8217;s simple, small, and has no dependencies:</p>
<pre><code>
declare = function(){
	var i, a = arguments, constructors = [];

	var Class = function(){
		for(i=0; i<=constructors.length-1 ; i++){
			constructors[i].apply(this, arguments);
		}
	}

	for(i=a.length-1;i>=0;i--){
		for(var n in a[i]){
			if(/^[A-Z]/.test(n)){ // testing for capitalized key words
				constructors.push(a[i][n]);
			}else{
				Class.prototype[n] = a[i][n];
			}
		}
	}
	return Class;
}
</code></pre>
<p>Declare expects one or more objects. It loops through the arguments, pulls the constructor out of each one based on a capitalized key word, then copies the properties and methods into the (temporary) Class function prototype. The array of constructors are then combined in reverse order so the last one fires first, allowing subsequent constructors to overwrite any properties the previous ones may have set. Finally, the Class function is then returned, which gives you your derived, custom class. When the custom class is fired, it loops through the child constructors and fires them all in the proper order.</p>
<h3>Fixing instanceof</h3>
<p>There is an issue in that <code>instanceof</code> doesn&#8217;t work with this pattern. But really, the only purpose for <code>instanceof</code> is to know what type of object you are in. For this implementation, saving the function names and testing for them works perfectly well. Remember, that&#8217;s why we are naming our constructors the same as the class names, and not using the same thing over and over, like &#8220;init&#8221;. We have a built-in ability for getting the class type, we just need to provide access to it.</p>
<p>Now, when the custom class is fired, it assigns the <code>classes</code> property:</p>
<pre><code>
declare = function(){
		var i, a = arguments, constructors = [], classes = [];

	var Class = function(){
		this.classes = classes;
		for(i=0; i<=constructors.length-1 ; i++){
			constructors[i].apply(this, arguments);
		}
	}

	Class.prototype = {
		isClass: function(cls){
			return this.classes[this.classes.length -1] === cls;
		},
		isSubclass: function(cls){
			for(var i=0; i &lt; this.classes.length; i++){
				if(this.classes[i] === cls) return true;
			}
			return false
		}
	};

	for(i=a.length-1;i>=0;i--){
		for(var n in a[i]){
			if(/^[A-Z]/.test(n)){
				constructors.push(a[i][n]);
				classes.push(n);
			}else{
				Class.prototype[n] = a[i][n];
			}
		}
	}

	return Class;
}
</code></pre>
<p>The <code>isClass</code> and <code>isSubclass</code> methods are added for convenience, so now we can test from where the instance was derived:</p>
<pre><code>
var o = declare(Foo, Bar, Zap);
console.log("is Foo:", o.isClass("Foo")); //true
console.log("has Bar:", o.isSubclass("Bar")); // true
</code></pre>
<h3>Conclusion</h3>
<p>I&#8217;ve written inheritance helpers before, but I was always concerned about extending existing classes, assembling both functions and objects, and therefore rearranging the arguments order. After I stopped worrying about these relatively needless features, the inheritance system became downright tiny! You are free to use and <a href="http://code.google.com/p/clubajax/source/browse/trunk/lang/declare.js">download it from Google Code</a>, or hopefully it just helped you to understand how simple inheritance in JavaScript really is.</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/simple-and-easy-javascript-inheritance/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Video: Hype Animation Tutorial</title>
		<link>http://clubajax.org/video-hype-animation-tutorial/</link>
		<comments>http://clubajax.org/video-hype-animation-tutorial/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 13:59:25 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[Meeting]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Hype]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1656</guid>
		<description><![CDATA[New goodies are posted! First, I gave a tutorial of the Hype HTML Animation App. You can see the video here, and you can see the final results here. And we also have Bob doing the Ajax News, where he focuses on an interesting blog and topic, The 11 JavaScript Mistakes You’re Making.]]></description>
			<content:encoded><![CDATA[<p><img src="http://clubajax.org/wp-content/uploads/2011/06/hype-example.jpg" alt="hype-example" title="hype-example" width="320" height="182" class="alignleft size-full wp-image-1657" /> New goodies are posted! First, I gave a tutorial of the <a href="http://tumultco.com/hype/">Hype HTML Animation App</a>. You can see <a href="http://clubajax.org/videos/hype-animation-tutorial/">the video here</a>, and you can see <a href="http://clubajax.org/files/hype/example.html">the final results here</a>.</p>
<p>And we also have Bob doing the Ajax News, where he focuses on an interesting blog and topic, <a href="http://net.tutsplus.com/tutorials/javascript-ajax/the-10-javascript-mistakes-youre-making/">The 11 JavaScript Mistakes You’re Making</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/video-hype-animation-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Next Meeting: Hype Demo</title>
		<link>http://clubajax.org/next-meeting-hype-demo/</link>
		<comments>http://clubajax.org/next-meeting-hype-demo/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 15:23:56 +0000</pubDate>
		<dc:creator>Mike Wilcox</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Meeting]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Hype]]></category>
		<category><![CDATA[meeting]]></category>

		<guid isPermaLink="false">http://clubajax.org/?p=1612</guid>
		<description><![CDATA[The next Club AJAX meeting will be next Tuesday, June 7th. We&#8217;ll demo the new Hype animation tool, do Ajax News, have some give-aways, and finally a code Q&#038;A. Yes. The Dallas Mavericks are playing game 4 of the finals that night. We looked at changing the date, but that didn&#8217;t work &#8211; the Mavericks [...]]]></description>
			<content:encoded><![CDATA[<p>The next <a href="http://clubajax.org/meetings/html5-hype-demo-and-ajax-qa/">Club AJAX meeting</a> will be next Tuesday, June 7th. We&#8217;ll demo the new <a href="http://tumultco.com/hype/">Hype animation tool</a>, do Ajax News, have some give-aways, and finally a code Q&#038;A.</p>
<p>Yes. The Dallas Mavericks are playing game 4 of the finals that night. We looked at changing the date, but that didn&#8217;t work &#8211; the Mavericks wouldn&#8217;t cooperate and move the game to Wednesday&#8230; something about TV schedules and 20,000 ticket holders.</p>
<p>Hence we&#8217;ve opted for an easy-going meeting, but it should still be fun! And I will have the Mavs game queuing up on my DVR and will be racing home to see them win! </p>
<h2 style="text-align:center;">GO MAVS!!</h2>
]]></content:encoded>
			<wfw:commentRss>http://clubajax.org/next-meeting-hype-demo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

