<?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>Blog - ShiftEleven &#187; JavaScript</title>
	<atom:link href="http://shifteleven.com/articles/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 19 Sep 2009 11:19:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-alpha</generator>
		<item>
		<title>Array-like Objects in JavaScript</title>
		<link>http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript</link>
		<comments>http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript#comments</comments>
		<pubDate>Fri, 29 Jun 2007 01:43:38 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[arguments]]></category>
		<category><![CDATA[array]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/?p=32</guid>
		<description><![CDATA[It make look like an Array, hell, it even has a length attribute; however, it may not actually be an Array.  JavaScript is sometimes a quirky language to say the least and the notion of what is an Array is no exception.
So what are these array-like objects that I speak of?  Well there [...]]]></description>
			<content:encoded><![CDATA[<p>It make look like an Array, hell, it even has a length attribute; however, it may not actually be an Array.  JavaScript is sometimes a quirky language to say the least and the notion of what is an Array is no exception.</p>
<p>So what are these array-like objects that I speak of?  Well there are a few of them, one of which is <code>arguments</code>.  <code>arguments</code> is a special variable that is available inside the body of every function.  It is in fact, the list of arguments that were passed in.</p>
<p><span id="more-32"></span></p>
<h3><code>arguments</code></h3>
<p>If you inspect the <code>arguments</code> variable in a tool such as <a href="http://www.getfirebug.com,">Firebug</a> you will notice that it prints out like an array.  It has elements in sequential order and it even has a <code>length</code> property.</p>
<pre class="ruby" title="code">var testFunction = function() {
  console.log(arguments);
  console.log(arguments.length);
};</pre>
<p>So what am I griping about?  Try doing <code>arguments.shift()</code>.  Uh-oh, looks like arguments.shift isn&#8217;t a function; but that&#8217;s a function of an Array.  Another bit of fun you can do is <code>console.log(arguments.constructor)</code>.  That will print to your console &#8220;Object()&#8221; while if you did <code>[].constructor</code>, that will print &#8220;Array()&#8221;.  Having fun yet?</p>
<p>This also isn&#8217;t just limited to <code>arguments</code>.  It seems that a lot of the DOM(document object model) collections are returned as these objects rather than arrays: <code>document.getElementsByTagName()</code>, <code>document.images</code>, <code>document.childNodes</code>.  In some cases, these array-not-arrays would be better suited to become arrays.</p>
<h3>Making the Array-like Objects become Arrays</h3>
<p>Well, that heading is a misnomer.  If we want those array-like objects to behave like arrays, we are going to need a new array.</p>
<pre class="ruby" title="code">var testFunction = function() {
  // Create a new array from the contents of arguments
  var args = Array.prototype.slice.call(arguments);

  var a = args.shift();
  console.log("The first argument is: %s", a);

  // send the remaining arguments to some other function
  someOtherFunction(args);
};</pre>
<p>Clearly, the magic is all in <code>Array.prototype.slice.call(arguments)</code>.  So let me break it down per piece.</p>
<dl>
<dt><code>Array</code></dt>
<dd>this is the name of the base object that we want</dd>
<dt><code>prototype</code></dt>
<dd>this can be thought of as the namespace for the instance methods of an array</dd>
<dt><code>slice</code></dt>
<dd>this extracts a section of an array and returns a new array, and without a beginning and ending index, it simply returns a copy of the array</dd>
<dt><code>call</code></dt>
<dd>this is a very useful function, it allows you to call a function from one object and use it in the context of another</dd>
</dl>
<p>Tada!  As a side note, if you are used to using <a href="http://www.prototypejs.org,">the prototype framework</a> then you can convert these array-like objects to arrays using <code>$A()</code>, even though Prototype does it&#8217;s array conversion differently.</p>
<p>Speaking of <code>$A</code>.  Let&#8217;s say you don&#8217;t like typing that long string code above and *you&#8217;re not using Prototype*.  Then you can create a shortcut just like the Prototype folks:</p>
<pre class="ruby" title="code">var $A = function(obj) {
  return Array.prototype.slice.call(obj);
};

// Example usage:
$A(document.getElementsByTagName("li"));</pre>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Create Objects in Object-Oriented JavaScript with Prototype</title>
		<link>http://shifteleven.com/articles/2007/04/29/how-to-create-objects-in-object-oriented-javascript-with-prototype</link>
		<comments>http://shifteleven.com/articles/2007/04/29/how-to-create-objects-in-object-oriented-javascript-with-prototype#comments</comments>
		<pubDate>Sun, 29 Apr 2007 10:40:22 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/?p=28</guid>
		<description><![CDATA[Javascript Kata has a teriffic an article on how to create objects with JavaScript the old fashion way, without any JavaScript libraries.  But I use prototype and it has its very own way to create a classes.

Creating an empty class
In keeping with the cat theme of Javascript Kata, I will create my own Cat [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.javascriptkata.com">Javascript Kata</a> has a teriffic <a href="http://www.javascriptkata.com/2007/03/23/how-to-create-objects-in-object-oriented-javascript/">an article on how to create objects with JavaScript</a> the old fashion way, without any JavaScript libraries.  But I use <a href="http://www.prototypejs.org/">prototype</a> and it has its very own way to create a classes.</p>
<p><span id="more-28"></span></p>
<h3>Creating an empty class</h3>
<p>In keeping with the cat theme of Javascript Kata, I will create my own <code>Cat</code> class.  This is very easy to do; you simply create a new class and assign it to a variable.</p>
<pre class="javascript" title="code">  var Cat = Class.create();</pre>
<h3>Creating the constructor</h3>
<p>Unlike the Javascript Kata example, the constructor is not set inside of the function which created the class.  Instead it is put into the prototype attribute of the class, under the special method name <code>initialize</code>.</p>
<pre class="javascript" title="code">  var Cat = Class.create();
  Cat.prototype = {
    initialize: function(name) {
      this.name = name;
    }
  };</pre>
<h3>Adding a instance method</h3>
<p>To add an instance method to our <code>Cat</code> class, all one has to do is add another property to the prototype definition.</p>
<pre class="javascript" title="code">  var Cat = Class.create();
  Cat.prototype = {
    initialize: function(name) {
      this.name = name;
    },
    meow: function() {
      alert(this.name + " : meow!");
    }
  };</pre>
<p>What I like about this is it keeps all references to <code>this</code> in one spot.</p>
<h3>Making the cat meow</h3>
<p>The way that the code is constructed is different; however, the way to instantiate our Cat class is exactly the same as it would be if you didn&#8217;t use prototype.</p>
<pre class="javascript" title="code">  var mistigri = new Cat('Mistigri');
  mistigri.meow();</pre>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2007/04/29/how-to-create-objects-in-object-oriented-javascript-with-prototype/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Firebug and the Console API</title>
		<link>http://shifteleven.com/articles/2007/02/09/firebug-and-the-console-api</link>
		<comments>http://shifteleven.com/articles/2007/02/09/firebug-and-the-console-api#comments</comments>
		<pubDate>Fri, 09 Feb 2007 10:34:26 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2007/02/09/firebug-and-the-console-api</guid>
		<description><![CDATA[Among the many Firefox extensions out there, there exists Firebug and it is       good.  It is truly an amazing piece of software.  There is a CSS(cascading style sheets) viewer, so that when you inspect an element in the DOM(document object model), you can view all of the [...]]]></description>
			<content:encoded><![CDATA[<p>Among the many <a href="https://addons.mozilla.org/firefox/extensions/">Firefox extensions</a> out there, there exists <a href="http://www.getfirebug.com,">Firebug</a> and it is       good.  It is truly an amazing piece of software.  There is a CSS(cascading style sheets) viewer, so that when you inspect an element in the DOM(document object model), you can view all of the CSS attributes that apply to that element.  Not only does it show you the applicable CSS rules, it breaks it down to the styles being used and to the styles which have been overwritten.  Very nice.  There are many other things it does, but one feature in particular that I don&#8217;t think I  would want to live witout is the console API(application programming interface).</p>
<p><span id="more-26"></span></p>
<h3>Console API</h3>
<p><code>alert('performing mouseover')</code> or <code>alert(myvalue)</code> is the first approach I took in order to get input on what was going on in the midst of my code.  Then I    wised up a little and made an debugging area in the HTML(hypertext markup language) where I would send those messages .  Well Firebug kicks that up a notch     with the  <code>console</code> object.  You can call one of several types of messages to write messages to the Firebug console: <code>log()</code>, <code>info()</code>, <code>debug()</code>, <code>warn()</code>,    and <code>error()</code>.</p>
<pre class="javascript" title="code">function dummyFunction(number) {
  if((number % 2) == 0)
    console.debug('even');
  else
    console.debug('odd');

  try {
    var t = 1 / number;
  catch(e) {
    console.error(e.toString());
  }
}</pre>
<p>Now you have a way of getting feedback about what you are doing without resorting to the dreaded <code>alert()</code> statement.  The power doesn&#8217;t stop here. <code>console.   dir(myObj)</code> will print out a metric-pony load&#8217;s worth of information about an object in your console for you.  If the object has an attribute, this will let    you see what it is, and what values are stored in that attribute.  Absolutely fabulous when you&#8217;re not sure exactly what an object is, or what it does.</p>
<h3>Units Tests NOT Be Damned</h3>
<p>Along with writing messages to the Firebug console, you can also write tests.  By using <code>console.assert()</code>, and a slew of <a href="http://cheat.errtheblog.com/s/firebug/">other firebug assertions</a>, you can start gaining a level of confidence in the code you write.</p>
<p>I just noticed the assertions today, so I haven&#8217;t used any of this just yet.  I imaging that one would create an HTML file, separate from all other pages on    the site, and create the tests in there.  If someone has done any assertion files, I would love to know your approach and any tips you may have.</p>
<h3>The Best Thing of All</h3>
<p>What could make this even better than it is right now?  Well, the answer is <a href="http://www.getfirebug.com/lite.html.">Firebug lite</a> Without it, the downside to    using Firebug is that other browsers, like Safari, Opera, and Internet Explorer, won&#8217;t know what do to with those commands and will just blow up in your face.  Firebug lite adds that Console API functionality to the other browsers.  By default, the Firebug lite console window stays hidden until you activate it.  Once  activated, all of you messages will be available to you.  All the more reason to write assertions, since you can now check your code quickly against all of your browsers.</p>
<p>If you don&#8217;t have this extension and you do JavasSript programming, or any other HTML coding really, then you need to get it right away and discover the all the other awesome aspects of Firebug.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2007/02/09/firebug-and-the-console-api/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mimicking the :hover Pseudo-class in IE</title>
		<link>http://shifteleven.com/articles/2006/12/14/mimicking-the-hover-pseudo-class-in-ie</link>
		<comments>http://shifteleven.com/articles/2006/12/14/mimicking-the-hover-pseudo-class-in-ie#comments</comments>
		<pubDate>Thu, 14 Dec 2006 21:30:37 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[pseudo-class]]></category>
		<category><![CDATA[suckerfish]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/?p=21</guid>
		<description><![CDATA[Internet Explorer frustrates the living hell out of me.  Event though IE 7 is finally out, it doesn&#8217;t mean that IE 6 can be ignored.  I really look forward to the day when my CSS(cascading style sheet) files aren&#8217;t full of unnecessary classes and hacks because the major browser vendors got it right [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ie7.com/">Internet Explorer</a> frustrates the living hell out of me.  Event though IE 7 is finally out, it doesn&#8217;t mean that IE 6 can be ignored.  I really look forward to the day when my CSS(cascading style sheet) files aren&#8217;t full of unnecessary classes and hacks because the major browser vendors got it right and because everyone has casted away the garbage.  Until then, I am still in search of tricks to simplifying my life.</p>
<p>One of these tricks is to make IE pretend like it knows how to use the <code>:hover</code> pseudo-class on all tags.</p>
<p>If you have ever tried to do a <a href="http://alistapart.com/articles/dropdowns/,">suckerfish dropdown</a> then you have gone through the same thing:  excitement as you have learned how to use structural markup to create a dropdown and then frustration as your work has been nullified by IE.  The fix involves creating duplicate <a href="http://css.maxdesign.com.au/selectutorial/">selectors</a> of those using <code>:hover</code>, but instead of using <code>:hover</code>, replace it with a class name <code>.hover</code>.  Making what are essentially duplicates and managing more content doesn&#8217;t sit well with me.  The other trick to getting suckerfish to work in IE is to use JavaScript.  The script will add a class definition to the tag when a user&#8217;s mouse if over the element and then removes the class when the users mouse is no longer over it.  Since this method uses JavaScript to solve one problem, why not make it solve both?</p>
<p><span id="more-21"></span></p>
<h3>The Code</h3>
<p>There is a requirement to using this code and that is you will need <a href="http://prototype.conio.net/.">prototype 1.5</a></p>
<pre class="javascript" title="code">var HoverBehavior = Class.create();
HoverBehavior.prototype = {
  initialize: function() {
    $A(document.styleSheets).each( function(stylesheet) {
      $A(stylesheet.rules).each( function(rule) {
        if( rule.selectorText.match(/:hover/i) ) {
          stylesheet.addRule( rule.selectorText.replace(/:hover/ig, '.hover'), rule.style.cssText );
        }
      });
    });

    $A(arguments).each( function(arg) {
      $$(arg).each( function(tag) {
        Event.observe(tag, 'mouseover', function() { Element.addClassName(tag, 'hover'); });
        Event.observe(tag, 'mouseout', function() { Element.removeClassName(tag, 'hover'); });
      });
    });
  }
};</pre>
<p>In summary, it loops through all of the <a href="http://www.javascriptkit.com/domref/stylesheet.shtml,">stylesheets</a> and for each stylesheet, it loops through all of the rules.  The first part of the initialization will only work in IE.  When looping through the rules, IE uses the <code>rules</code> attribute while others use <code>cssRules</code>.  Since this is only meant for IE, I am not going to bother making it cross-browser.</p>
<p>While looping through all of the rules, it is checking the selector text to see if <code>:hover</code> is in that text.  If so it will append a new CSS rule to the stylesheet.  The selector text for that rule is just like text of the matched selector; however, all instances of <code>:hover</code> have been replaced with <code>.hover</code>.  The new rule&#8217;s style is simply a copy of the matched selector.</p>
<p>That covers part 1 of the suckerfish fix, part 2 is to add the mouseover, mouseout states to the tags using <code>:hover</code>.</p>
<h3>Usage</h3>
<p><strong>After the page has been loaded</strong>, then it is time to create a new instance of the HoverBehavior class.  Since this is only for IE, it would be wise to use <a href="http://www.javascriptkit.com/howto/cc2.shtml">IE conditionals</a> as that your other users, who are using complaint browsers, shouldn&#8217;t have to process any unnecessary code.</p>
<pre class="html" title="code">&lt;!--[if lt IE 7]&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
//&lt;![CDATA[
  new HoverBehavior('li');
//]]&gt;
&lt;/script&gt;
&lt;![endif]--&gt;</pre>
<p>The constructor can take any number of arguments that you would give to prototype&#8217;s <code>$$</code> function, which are like CSS selectors themselves.  I could have also done something like:</p>
<ul>
<li> <code>new HoverBehavior('#nav li', '#footer li');</code></li>
<li><code>new HoverBehavior('li', 'p', 'span');</code></li>
<li> <code>new HoverBehavior('*');</code></li>
</ul>
<p>The reason I don&#8217;t assume the last one in that list is because I don&#8217;t want to kill the user&#8217;s browser.  Instead of being lazy and choosing all of the elements, I only want to put the <code>mouseover</code>, <code>mouseout</code> functionality on the elements that need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/12/14/mimicking-the-hover-pseudo-class-in-ie/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Engrish Javascript</title>
		<link>http://shifteleven.com/articles/2006/10/20/engrish-javascript</link>
		<comments>http://shifteleven.com/articles/2006/10/20/engrish-javascript#comments</comments>
		<pubDate>Sat, 21 Oct 2006 02:08:22 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[engrish]]></category>
		<category><![CDATA[Kim Jong Il]]></category>
		<category><![CDATA[racism]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[Team America]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/articles/2006/10/20/engrish-javascript</guid>
		<description><![CDATA[In what I can think of as the most important JavaScript that I have ever written, I have made something which will parse the DOM(Document Object Model) and replace all of the &#8216;l&#8217;s with &#8216;r&#8217;s.  This idea came about after watching Team America: World Police about 1,000 times and after Kim Jong Il gave [...]]]></description>
			<content:encoded><![CDATA[<p>In what I can think of as the most important JavaScript that I have ever written, I have made something which will parse the DOM(Document Object Model) and replace all of the &#8216;l&#8217;s with &#8216;r&#8217;s.  This idea came about after watching <a href="http://www.rottentomatoes.com/m/team_america_world_police/">Team America: World Police</a> about 1,000 times and after <a href="http://en.wikipedia.org/wiki/Kim_Jong-il">Kim Jong Il</a> gave his <a href="http://www.theage.com.au/news/World/Kim-Jongil-sorry-about-nuclear-test/2006/10/20/1160851096275.html">apology</a> for doing some nuke testing. Back to the script:</p>
<pre class="javascript" title="code">function engrish(n) {
	if(n.nodeType == document.TEXT_NODE) {
		n.nodeValue = n.nodeValue.replace(/l/g, 'r').replace(/L/g, 'R');
	} else if(n.hasChildNodes()) {
		for(var i=0; i&lt;n.childNodes.length; i++) {
			engrish(n.childNodes[i]);
		}
	}
}</pre>
<p>To execute this code, simply call <code>engrish(document.documentElement);</code>.  Using <a href="http://www.htmlgoodies.com/primers/jsp/article.php/3622321,">recursion</a> this function goes through the DOM, finds text nodes and replaces the characters.</p>
<p><span id="more-16"></span></p>
<p>This is fine an dandy if you put this on your site so you can quickly convert your copy to <a href="http://www.engrish.com/detail.php?imagename=biohazard.jpg&amp;category=Signs/Posters&amp;date=2003-10-16;">engrish</a> however there is something that you can do to turn any page into it&#8217;s <a href="http://www.engrish.com/detail.php?imagename=dewdew.jpg&amp;category=Candy&amp;date=1996-10-18">engrish</a> form.  You can save this code as a <a href="http://mozilla.gunnars.net/firefox_bookmarks_tutorial.html.">bookmark</a></p>
<p>If you would like an example of what the bookmark URL would be, just <a href="javascript:function engrish(n) {if(n.nodeType == document.TEXT_NODE) {n.nodeValue = n.nodeValue.replace(/l/g, 'r').replace(/L/g, 'R');} else if(n.hasChildNodes()) {for(var i=0; i&lt;n.childNodes.length; i++) {engrish(n.childNodes[i]);}}} engrish(document.documentElement);">engrish-ify</a> this page and save the link as a bookmark (you can drag that link into your bookmark toolbar in <a href="http://www.mozilla.com/firefox/)">Firefox</a></p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2006/10/20/engrish-javascript/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
