<?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>Karmagination &#187; Javascript</title>
	<atom:link href="http://www.karmagination.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.karmagination.com/blog</link>
	<description>Creation begins with imagination</description>
	<lastBuildDate>Fri, 25 Sep 2009 03:44:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Karmic Flow 0.1 &#8211; Content Slider for Karmagination / jQuery</title>
		<link>http://www.karmagination.com/blog/2009/09/10/karmic-flow-0-1-content-slider-for-karmagination-jquery/</link>
		<comments>http://www.karmagination.com/blog/2009/09/10/karmic-flow-0-1-content-slider-for-karmagination-jquery/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 23:13:08 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Karmagination]]></category>
		<category><![CDATA[Karmic Flow]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/blog/?p=108</guid>
		<description><![CDATA[Note: I have released a new version of Karmic Flow here. Please click to get the latest Karmic Flow. This is a remake of my wildly popular plugin for jQuery, known as jFlow. It works with Karmagination 0.2 or jQuery 1.3.2. Download or see a demo with Karmagination or see a demo with jQuery Basic [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F09%2F10%2Fkarmic-flow-0-1-content-slider-for-karmagination-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F09%2F10%2Fkarmic-flow-0-1-content-slider-for-karmagination-jquery%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de" height="61" width="50" /><br />
			</a>
		</div>
<h2>Note: I have released a new version of Karmic Flow <a href="http://www.karmagination.com/blog/2009/09/24/karmic-flow-0-2-content-slider-for-karmagination-jquery/">here</a>. Please click to get the latest Karmic Flow.</h2>
<p>This is a remake of my wildly popular plugin for jQuery, known as <a href="http://www.gimiti.com/kltan/wordpress/?p=46">jFlow</a>. It works with <a href="/downloads/karma-0.2.zip">Karmagination 0.2</a> or jQuery 1.3.2. </p>
<p><a href="/downloads/karmic_flow-0.1.zip">Download</a> or see a <a href="/demo/kFlow/0.1/example-karma.html">demo with Karmagination</a> or see a <a href="/demo/kFlow/0.1/example-jq.html">demo with jQuery</a><br />
<img align="top" src="/demo/kFlow/0.1/i/optimized.png" title="Optimized for Karmagination" class="alignnone" width="142" height="20" /></p>
<p><img src="/demo/kFlow/0.1/demo.jpg" alt="Karmic Flow Demo" /></p>
<p>Basic usage</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">karmicFlow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Here are the configurable options:</h2>
<p>duration: (int) the time in milliseconds it takes to move 1 slide<br />
timer: (int) waiting time in milliseconds for each slide before it slides, used when slides are played<br />
auto: (boolean) specify whether to automatically play the slides on load</p>
<p><strong>The options below are not to be modified unless you know what you are doing. If you change the options below make sure you change the corresponding class name for the HTML.</strong></p>
<p>container: (string) the class name for container of slider and slides<br />
slider: (string) the class name for slider<br />
slides: (string) the class name for slides<br />
slide_selected: (string) the class name added to currect active slide<br />
slide_overflow: (string) the class name that handles how to display overflow slide<br />
controller: (string) the class name for controller that slides to a particular slides<br />
controller_selected: (string) the class name added to the controller of the current active slide<br />
next: (string) the class name that slides the slider to the next slide<br />
prev: (string) the class name that slides the slider to the previous slide<br />
play: (string) the class name that automatically slides slide in timed intervals<br />
pause: (string) the class name that pauses the effects of play</p>
<h2>The default values for the options:</h2>
<p>duration: 300<br />
timer: 2500<br />
auto: false<br />
container: &#8216;karmic_flow_container&#8217;<br />
slider: &#8216;karmic_flow_slider&#8217;<br />
slides: &#8216;karmic_flow_slides&#8217;<br />
sliding: &#8216;karmic_flow_sliding&#8217;<br />
slide_selected: &#8216;karmic_flow_slide_selected&#8217;<br />
slide_overflow: &#8216;karmic_flow_slide_overflow&#8217;<br />
controller: &#8216;karmic_flow_controller&#8217;<br />
controller_selected: &#8216;karmic_flow_controller_selected&#8217;<br />
next: &#8216;karmic_flow_next_controller&#8217;<br />
prev: &#8216;karmic_flow_prev_controller&#8217;<br />
play: &#8216;karmic_flow_play_controller&#8217;<br />
pause: &#8216;karmic_flow_pause_controller&#8217;</p>
<h2>Recommended HTML structure:</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_prev_controller&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span> target<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span><span style="color: #339933;">&gt;</span>Prev<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_next_controller&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span> target<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span><span style="color: #339933;">&gt;</span>Next<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_play_controller karmic_flow_pause_controller&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span> target<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span><span style="color: #339933;">&gt;</span>Play<span style="color: #339933;">/</span>Pause<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_controller karmic_flow_controller_selected&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#slide1&quot;</span> target<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span><span style="color: #339933;">&gt;</span>Leaves<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_controller&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#slide2&quot;</span> target<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span><span style="color: #339933;">&gt;</span>Sand<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_controller&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#slide3&quot;</span> target<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span><span style="color: #339933;">&gt;</span>Beach<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_controller&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#slide4&quot;</span> target<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span><span style="color: #339933;">&gt;</span>Cheetah<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;container&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_container&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>ul <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_slider&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>li <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_slides karmic_flow_slide_selected&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slide1&quot;</span><span style="color: #339933;">&gt;&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;i/p1.jpg&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>li <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_slides&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slide2&quot;</span><span style="color: #339933;">&gt;&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;i/p2.jpg&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>li <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_slides&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slide3&quot;</span><span style="color: #339933;">&gt;&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;i/p3.jpg&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>li <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;karmic_flow_slides&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;slide4&quot;</span><span style="color: #339933;">&gt;&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;i/p4.jpg&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/09/10/karmic-flow-0-1-content-slider-for-karmagination-jquery/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Work with gzip, not against it</title>
		<link>http://www.karmagination.com/blog/2009/09/01/work-with-gzip-not-against-it/</link>
		<comments>http://www.karmagination.com/blog/2009/09/01/work-with-gzip-not-against-it/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 00:05:44 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[gzip]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/blog/?p=86</guid>
		<description><![CDATA[Below is the gzip algorithm in plain text, taken from http://www.gzip.org/. Why do we need to study it? Read below for the answer. The deflation algorithm used by gzip (also zip and zlib) is a variation of LZ77 (Lempel-Ziv 1977, see reference below). It finds duplicated strings in the input data. The second occurrence of [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F09%2F01%2Fwork-with-gzip-not-against-it%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F09%2F01%2Fwork-with-gzip-not-against-it%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de" height="61" width="50" /><br />
			</a>
		</div>
<p>Below is the gzip algorithm in plain text, taken from http://www.gzip.org/. Why do we need to study it? Read below for the answer.</p>
<pre>The deflation algorithm used by gzip (also zip and zlib) is a variation of
LZ77 (Lempel-Ziv 1977, see reference below). It finds duplicated strings in
the input data.  The second occurrence of a string is replaced by a
pointer to the previous string, in the form of a pair (distance,
length).  Distances are limited to 32K bytes, and lengths are limited
to 258 bytes. When a string does not occur anywhere in the previous
32K bytes, it is emitted as a sequence of literal bytes.  (In this
description, `string' must be taken as an arbitrary sequence of bytes,
and is not restricted to printable characters.)

Literals or match lengths are compressed with one Huffman tree, and
match distances are compressed with another tree. The trees are stored
in a compact form at the start of each block. The blocks can have any
size (except that the compressed data for one block must fit in
available memory). A block is terminated when deflate() determines that
it would be useful to start another block with fresh trees. (This is
somewhat similar to the behavior of LZW-based _compress_.)

Duplicated strings are found using a hash table. All input strings of
length 3 are inserted in the hash table. A hash index is computed for
the next 3 bytes. If the hash chain for this index is not empty, all
strings in the chain are compared with the current input string, and
the longest match is selected.

The hash chains are searched starting with the most recent strings, to
favor small distances and thus take advantage of the Huffman encoding.
The hash chains are singly linked. There are no deletions from the
hash chains, the algorithm simply discards matches that are too old.

To avoid a worst-case situation, very long hash chains are arbitrarily
truncated at a certain length, determined by a runtime option (level
parameter of deflateInit). So deflate() does not always find the longest
possible match but generally finds a match which is long enough.

deflate() also defers the selection of matches with a lazy evaluation
mechanism. After a match of length N has been found, deflate() searches for
a longer match at the next input byte. If a longer match is found, the
previous match is truncated to a length of one (thus producing a single
literal byte) and the process of lazy evaluation begins again. Otherwise,
the original match is kept, and the next match search is attempted only N
steps later.

The lazy match evaluation is also subject to a runtime parameter. If
the current match is long enough, deflate() reduces the search for a longer
match, thus speeding up the whole process. If compression ratio is more
important than speed, deflate() attempts a complete second search even if
the first match is already long enough.

The lazy match evaluation is not performed for the fastest compression
modes (level parameter 1 to 3). For these fast modes, new strings
are inserted in the hash table only when no match was found, or
when the match is not too long. This degrades the compression ratio
but saves time since there are both fewer insertions and fewer searches.
</pre>
<p>&nbsp;</p>
<p>In our quest for the best possible JavaScript file size, we have always tried to optimize our code for minification. BUT, better minification does not equate to better gzip compression. Here&#8217;s what you need to avoid to get a better gzip compression, do not munge strings.</p>
<p>Recently there have been some libraries trying this strategy: using array notation instead of the dot notation for members of Objects. Here is a perfect illustration.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// uncompressed</span>
<span style="color: #003366; font-weight: bold;">var</span> getElementById <span style="color: #339933;">=</span> <span style="color: #3366CC;">'getElementById'</span><span style="color: #339933;">;</span>
document<span style="color: #009900;">&#91;</span>getElementById<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'blue'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// minified</span>
<span style="color: #003366; font-weight: bold;">var</span> a<span style="color: #339933;">=</span><span style="color: #3366CC;">'getElementById'</span><span style="color: #339933;">;</span>document<span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'blue'</span><span style="color: #339933;">;</span></pre></div></div>

<p>And you have read gzip&#8217;s algorithm, right? What does it do? Well, it maintains a lookup table for strings (note: everything is treated as strings in gzip&#8217;s point of view). By munging strings, you are actually emulating gzip in some ways. Here&#8217;s what the code above just did, added &#8216;a&#8217; to the gzip lookup table, instead of just having getElementById there.</p>
<p>I have created a very simple case of string munging. Here&#8217;s the proof.</p>
<p>In the uncompressed file I have the following iterated till line 1000.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'u1'</span>
<span style="color: #3366CC;">'u2'</span>
<span style="color: #3366CC;">'u3'</span>
<span style="color: #3366CC;">'u4'</span>
<span style="color: #3366CC;">'u5'</span>
<span style="color: #3366CC;">'u6'</span>
<span style="color: #3366CC;">'u7'</span>
<span style="color: #3366CC;">'u8'</span>
<span style="color: #3366CC;">'u9'</span>
<span style="color: #3366CC;">'u10'</span></pre></div></div>

<p>&nbsp;<br />
In the munged file, I have the following iterated till line 1001</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u1'</span><span style="color: #339933;">,</span> b <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u2'</span><span style="color: #339933;">,</span> c <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u3'</span><span style="color: #339933;">,</span> d <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u4'</span><span style="color: #339933;">,</span> e <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u5'</span><span style="color: #339933;">,</span> f <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u6'</span><span style="color: #339933;">,</span> g <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u7'</span><span style="color: #339933;">,</span> h <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u8'</span><span style="color: #339933;">,</span> i <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u9'</span><span style="color: #339933;">,</span> j <span style="color: #339933;">=</span> <span style="color: #3366CC;">'u10'</span>
a
b
c
d
e
f
g
h
i
j</pre></div></div>

<p>&nbsp;<br />
The results are, taken from <a href="http://www.karmagination.com/downloads/gzip_munging/">here</a> : </p>
<ul>
<li>uncompressed &#8211; 5,099 bytes</li>
<li>munged &#8211; 2,103 bytes</li>
<li>uncompressed gzipped &#8211; 93 bytes</li>
<li>munged gzipped &#8211; 120 bytes</li>
</ul>
<p>My experiments conclude the following, <strong>string munging is perfectly fine if you are munging less than 3 different unique strings and it yields better result minification-wise</strong>. Otherwise, leave the strings as is, and save yourself some bytes (gzipped). <strong>Suprised</strong>? <img src='http://www.karmagination.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/09/01/work-with-gzip-not-against-it/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Browser detection is still useful</title>
		<link>http://www.karmagination.com/blog/2009/07/31/browser-detection-is-still-useful/</link>
		<comments>http://www.karmagination.com/blog/2009/07/31/browser-detection-is-still-useful/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 00:21:19 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[browser detection]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/blog/?p=73</guid>
		<description><![CDATA[Note: Whether you agree or not to this article please retweet and spread the word so that we can promote a healthy discussion here. Here is what I think happened in the last 10 plus years or so in the cross browser scripting world; pure incompetence -> global object detection (i.e. document.all) -> user agent [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F31%2Fbrowser-detection-is-still-useful%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F31%2Fbrowser-detection-is-still-useful%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de" height="61" width="50" /><br />
			</a>
		</div>
<p>Note: Whether you agree or not to this article please retweet and spread the word so that we can promote a healthy discussion here.</p>
<p>Here is what I think happened in the last 10 plus years or so in the cross browser scripting world; pure incompetence -> global object detection (i.e. document.all) -> user agent sniffing -> feature detection (i.e. document.querySelectorAll) -> <a href="http://thinkweb2.com/projects/prototype/feature-detection-all-the-way/">feature simulation</a> -> browser detect + feature detection / feature simulation</p>
<p>Here&#8217;s some take on <a href="http://blog.davglass.com/2009/01/browser-sniffing-vs-object-detection/">object detection vs user agent sniffing</a> which I am not going to discussing in this article.</p>
<p>Browser detect, whether using user agent sniffing or global object detection, is notorious for being unreliable because are highly unpredictable or can be hijacked.</p>
<p>Feature detect lets you know whether the browser has the method but does not tell you if it works correctly.</p>
<p>Feature simulation, will not only detect if the browser supports a certain method but also tells you if it works correctly or not.</p>
<p>It is clear that, feature simulation is the only way to go in future. End of story. Not!</p>
<p><strong>So, where does browser detect come into play?</strong></p>
<p>In modern JavaScript, browser detect has lost its place in cross-browser scripting and there&#8217;s no doubt about that. BUT, browsers are built differently, from different people with different personalities and philosophies and with different tools. Although browsers try their best to adhere to all the standards, the standards does not specify how implement them i.e. which algorithm to use, how to allocate/deallocate etc.</p>
<p>There exists different codes that will achieve the same results. Let&#8217;s look at two pieces of code that delete all descendant nodes from an element.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">&gt;&lt;</span>strong<span style="color: #339933;">&gt;</span>A<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;</span>strong<span style="color: #339933;">&gt;</span>A<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;</span>strong<span style="color: #339933;">&gt;</span>A<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// method one</span>
<span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> document.<span style="color: #660066;">geElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
node.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// method two</span>
<span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> document.<span style="color: #660066;">geElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> fragment <span style="color: #339933;">=</span> document.<span style="color: #660066;">createDocumentFragment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> newEl <span style="color: #339933;">=</span> node.<span style="color: #660066;">cloneNode</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fragment.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
node.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">replaceChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #339933;">,</span> node<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The benchmark results are <a href="http://blog.stevenlevithan.com/archives/faster-than-innerhtml">here</a>. Both of the code snippets are cross-browser compatible. The Gecko engine runs faster using method two, other browser runs faster using method one. Here&#8217;s how I would use browser detect to do performance optimization.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> document.<span style="color: #660066;">geElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> fragment<span style="color: #339933;">,</span> newEl<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isGecko<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  fragment <span style="color: #339933;">=</span> document.<span style="color: #660066;">createDocumentFragment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  newEl <span style="color: #339933;">=</span> node.<span style="color: #660066;">cloneNode</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  fragment.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  node.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">replaceChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #339933;">,</span> node<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  node.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It does not matter whether isGecko returns a false positive or false negative, the code just works. Below is a generalization on how to use browser detect to optimize your code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isSomeBrowser <span style="color: #339933;">&amp;&amp;</span> isSomeBrowserFeatureSupported<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isSomeCrossBrowserFunctionSupported<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To illustrate the performance improvements, here&#8217;s a benchmark using <a href="http://www.karmagination.com/benchmark">Karmagination vs other JS libraries</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/07/31/browser-detection-is-still-useful/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Even Faster JavaScript Trim</title>
		<link>http://www.karmagination.com/blog/2009/07/30/even-faster-javascript-trim/</link>
		<comments>http://www.karmagination.com/blog/2009/07/30/even-faster-javascript-trim/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 00:15:59 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[trim]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/blog/?p=69</guid>
		<description><![CDATA[Before following this article, I suggest that you read Steven Levithan&#8217;s blog on this topic. The most recent Firefox 3.5 release supports JavaScript 1.8.1. The language came with the trim, trimLeft, trimRight methods for string. Here&#8217;s how to use it. if &#40;String.prototype.trim&#41; ' a '.trim&#40;&#41;; The older faster and reliable way of doing it is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F30%2Feven-faster-javascript-trim%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F30%2Feven-faster-javascript-trim%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de" height="61" width="50" /><br />
			</a>
		</div>
<p>Before following this article, I suggest that you read <a href="http://blog.stevenlevithan.com/archives/faster-trim-javascript">Steven Levithan&#8217;s blog on this topic</a>.</p>
<p>The most recent Firefox 3.5 release supports JavaScript 1.8.1. The language came with the trim, trimLeft, trimRight methods for string. Here&#8217;s how to use it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#41;</span>
  <span style="color: #3366CC;">' a '</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The older faster and reliable way of doing it is simply do a string replace on both the left and right.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">' a '</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^[\s\xA0]+/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[\s\xA0]+$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here&#8217;s a sample code to benchmark how they fare against each other.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">200000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #3366CC;">' a '</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
end <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #339933;">;</span>
&nbsp;
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'trim method uses '</span> <span style="color: #339933;">+</span> end <span style="color: #339933;">+</span> <span style="color: #3366CC;">' ms&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">200000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #3366CC;">' a '</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^[\s\xA0]+/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[\s\xA0]+$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
end <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #339933;">;</span>
&nbsp;
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'replace method uses '</span> <span style="color: #339933;">+</span>end <span style="color: #339933;">+</span> <span style="color: #3366CC;">' ms&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">200000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #3366CC;">' a '</span>.<span style="color: #660066;">trimLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trimRight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
end <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #339933;">;</span>
&nbsp;
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'trimLeft then trimRight method uses '</span> <span style="color: #339933;">+</span> end <span style="color: #339933;">+</span> <span style="color: #3366CC;">' ms&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Running the benchmark resulted in:<br />
.trim() 16ms<br />
.replace() 303ms<br />
.trimLeft().trimRight() 30ms</p>
<p>That&#8217;s a superb 20x improvement in this case, and I highly recommend its usage. The code below will take care of browsers that do not support trim.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> trim <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> str.<span style="color: #660066;">trim</span> <span style="color: #339933;">?</span> str.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> str.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^[\s\xA0]+/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[\s\xA0]+$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/07/30/even-faster-javascript-trim/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tricking firebug to display an Object as an Array</title>
		<link>http://www.karmagination.com/blog/2009/07/30/tricking-firebug-to-display-an-object-as-an-array/</link>
		<comments>http://www.karmagination.com/blog/2009/07/30/tricking-firebug-to-display-an-object-as-an-array/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 19:44:02 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Object]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/blog/?p=58</guid>
		<description><![CDATA[In real world applications, there are instances where the data structure of an object looks like an array. var obj = &#123; 0: 'apple', 1: 'orange', 2: 'crab', length: 3 &#125;; &#160; var ary = &#91;'apple', 'orange', 'crab'&#93;; &#160; alert&#40;obj&#91;0&#93;&#41;; // apple alert&#40;ary&#91;0&#93;&#41;; // apple &#160; alert&#40;obj.length&#41;; // 3 alert&#40;ary.length&#41;; // 3 They share some [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F30%2Ftricking-firebug-to-display-an-object-as-an-array%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F30%2Ftricking-firebug-to-display-an-object-as-an-array%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de" height="61" width="50" /><br />
			</a>
		</div>
<p>In real world applications, there are instances where the data structure of an object looks like an array.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'apple'</span><span style="color: #339933;">,</span>
 <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">,</span>
 <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'crab'</span><span style="color: #339933;">,</span>
 length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ary <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'apple'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'crab'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// apple</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ary<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// apple</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 3</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ary.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 3</span></pre></div></div>

<p>They share some similarities, and, for <strong>debugging purposes</strong> (in Firebug), it&#8217;s sometimes beneficial display these special type of Objects as Arrays. Here&#8217;s how to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// using the example above</span>
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'apple'</span><span style="color: #339933;">,</span>
  <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">,</span>
  <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'crab'</span><span style="color: #339933;">,</span>
  length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
  push<span style="color: #339933;">:</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">push</span><span style="color: #339933;">,</span>
  slice<span style="color: #339933;">:</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span><span style="color: #339933;">,</span>
  splice<span style="color: #339933;">:</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">splice</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">console</span> <span style="color: #339933;">&amp;&amp;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#41;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// ['apple', 'orange', 'crab'];</span></pre></div></div>

<p>You can include other Array prototypes but push, slice and splice are the bare minimum to trick Firebug. </p>
<p>Note: The Objects are not converted to Arrays. I believe Firebug uses <a href="http://thinkweb2.com/projects/prototype/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/">duck-typing</a> to detect Arrays when displaying them on the console.</p>
<p>Updated: I guess my guess is correct, below is the proof.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// using the example above</span>
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'apple'</span><span style="color: #339933;">,</span>
  <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">,</span>
  <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'crab'</span><span style="color: #339933;">,</span>
  length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
  push<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  slice<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  splice<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">console</span> <span style="color: #339933;">&amp;&amp;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#41;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// ['apple', 'orange', 'crab'];</span></pre></div></div>

<p>Update 2: Looking at the Firebug source that Tobie Langel provided in the comment below, we only need to include splice.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// using the example above</span>
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'apple'</span><span style="color: #339933;">,</span>
  <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">,</span>
  <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'crab'</span><span style="color: #339933;">,</span>
  length<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
  splice<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">console</span> <span style="color: #339933;">&amp;&amp;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#41;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// ['apple', 'orange', 'crab'];</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/07/30/tricking-firebug-to-display-an-object-as-an-array/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Advanced Javascript &#8211; Object, Array and Literals</title>
		<link>http://www.karmagination.com/blog/2009/07/29/javascript-kung-fu-object-array-and-literals/</link>
		<comments>http://www.karmagination.com/blog/2009/07/29/javascript-kung-fu-object-array-and-literals/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 01:41:55 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Literal]]></category>
		<category><![CDATA[Object]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/?p=3</guid>
		<description><![CDATA[What are literals? You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script. In this article, I am going to focus on Array and Object literals to show you the expressiveness and flexibility of JavaScript.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F29%2Fjavascript-kung-fu-object-array-and-literals%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F29%2Fjavascript-kung-fu-object-array-and-literals%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de" height="61" width="50" /><br />
			</a>
		</div>
<p>What are literals? You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script. In this article, I am going to focus on Array and Object literals to show you the expressiveness and flexibility of JavaScript.</p>
<h2>Object vs Array</h2>
<p>There are a few differences between Object and Array. </p>
<ol>
<li>Their prototyped methods differs.</li>
<li>Object does not have a built-in length, Array’s length is the number of items it has.</li>
<li>Arrays auto increment their keys (in most cases), Object behaves like associative Arrays (meaning: you define the keys).</li>
<li>New Objects use the {} literal and new Arrays use the [] literal.</li>
</ol>
<p>There are a few similarities too. </p>
<ol>
<li>The typeof Array and Object is “object”. This causes headaches for many developers.</li>
<li>You can store multiple values(data types) in Array and Object.</li>
</ol>
<h2>Object + Array vs {} + [] </h2>
<p>Similarities, read further below for proof.</p>
<ol>
<li>In their native forms. Array and Object instances share the same constructor as [] and {}.</li>
<li>Literals are shorter representation for Array and Object instances. (Meaning: {} is equivalent to new Object() and [] is equivalent to new Array().</li>
</ol>
<p>Differences.</p>
<ol>
<li>{} and []&#8216;s constructor cannot be overwritten.</li>
<li>Object and Array are constructors but {} and [] are instances.</li>
</ol>
<h2>Creating instances of Object and Array</h2>
<p>The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> arry <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can use literals to create instances too.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//verify obj</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj <span style="color: #000066; font-weight: bold;">instanceof</span> Object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts true, revisiting below</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ary <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//verify ary</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ary <span style="color: #000066; font-weight: bold;">instanceof</span> Array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts true</span></pre></div></div>

<h2>Populating Object and Array</h2>
<p>You can do it during initialization.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Tim Burtonic&quot;</span><span style="color: #339933;">,</span>
  age<span style="color: #339933;">:</span> <span style="color: #CC0000;">16</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Tim Burtonic</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ary <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Sugar'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Salt'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Oil'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ary<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Sugar</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ary2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sugar'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Salt'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Oil'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ary<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Salt</span></pre></div></div>

<p>You can do it after initialization.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
obj.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Tim Burtonic&quot;</span><span style="color: #339933;">;</span>
obj.<span style="color: #660066;">age</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">;</span>
obj<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;sex&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;male&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// equivalent to obj.sex = &quot;male&quot;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ary <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ary.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sugar'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Salt'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Oil'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ary<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Pepper'</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Detecting Object and Array</h2>
<p>Let’s start with the intuitive typeof method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts object</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ary <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> ary<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts object</span></pre></div></div>

<p>The typeof method is not helpful in this situation. Why not instanceof or constructor?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj <span style="color: #000066; font-weight: bold;">instanceof</span> Object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ary <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ary <span style="color: #000066; font-weight: bold;">instanceof</span> Array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ary.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre></div></div>

<p>Now this looks promising, but let us test further. Supposing you have an iframe checking whether it’s parent’s variable is an Array.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// in parent window</span>
<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//inside the iframe</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>parent.<span style="color: #660066;">window</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns array</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>parent.<span style="color: #660066;">window</span>.<span style="color: #660066;">b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns object</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>parent.<span style="color: #660066;">window</span>.<span style="color: #660066;">a</span> <span style="color: #000066; font-weight: bold;">instanceof</span> Array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>parent.<span style="color: #660066;">window</span>.<span style="color: #660066;">b</span> <span style="color: #000066; font-weight: bold;">instanceof</span> Object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>parent.<span style="color: #660066;">window</span>.<span style="color: #660066;">a</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>parent.<span style="color: #660066;">window</span>.<span style="color: #660066;">b</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">===</span> Object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></div></div>

<p>The array and object failed the instanceof test. Why? Each frame has it’s own copy of natives and globals, while the code and functionality is the same across frames (if not overloaded), the instances are not using the same copy of constructor function, that’s why the test failed. </p>
<p>Around 2008, <a href="http://www.caplet.com/">Mark Miller</a> came out with the a great idea by scrutinizing the EcmaScript specification. Let’s see what the <a href="http://profiles.yahoo.com/blog/GSBHPXZFNRM2QRAP3PXNGFMFVU?eid=fam48bo6nChhLpXTWLYuo2PoctbJjTIo34SjoLBF9VV3glXt.w">Miller device</a> looks like.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> isArray <span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;[object Array]&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
isArray<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns true;</span></pre></div></div>

<p>This niffy piece of code works across frames too! But this turns out to be only as reliable as Object itself being native. Detecting natives is not as trivial as it seems, according to <a href="http://thinkweb2.com/projects/prototype/detecting-built-in-host-methods/">Juriy Zaytsev</a>. Let us make this fail badly.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// native prototype overloaded, some js libraries extends them</span>
Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span><span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span>  <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> isArray <span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>isArray<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns true, expecting false;</span></pre></div></div>

<h2>Reverting to native Object and Array</h2>
<p>The code below overloads the Object&#8217;s native prototype.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// just overwriting the prototype</span>
Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span><span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> o<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// using prototype</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'good day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts good day</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// using new instance</span>
<span style="color: #003366; font-weight: bold;">var</span> o1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>o1.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'good day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts good day</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// using literal instance</span>
<span style="color: #003366; font-weight: bold;">var</span> o2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>o2.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'good day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts good day</span></pre></div></div>

<p>This overloads the Object&#8217;s constructor and native prototype.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Object <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span><span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> o<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// what is Object now</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>Object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// directly using prototype call</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'good day'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'good day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts good day</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// using new Object</span>
<span style="color: #003366; font-weight: bold;">var</span> o1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>o1.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'good day'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'good day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts good day</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// using literal</span>
<span style="color: #003366; font-weight: bold;">var</span> o2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>o2.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'good day'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'good day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts [object String]</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Object is being reverted to it's native form;</span>
Object <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>.<span style="color: #660066;">constructor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> o3 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>o3.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'good day'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'good day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts [object String]</span></pre></div></div>

<p>It seems that when a native constructor is overwritten, its literal will request a new copy of the native constructor and prototypes. Using this result, I will try to reset Object and Array to their native versions.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// overwriting the constructor and prototype</span>
<span style="color: #006600; font-style: italic;">// overwritten prototype</span>
Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'[I am overloaded toString]'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">sort</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'[I am overloaded sort]'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts [I am overloaded toString]</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'c'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'b'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts [I am overloaded sort]</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// function declaration instead of expression must be used because we want it to execute first globally</span>
<span style="color: #006600; font-style: italic;">// do not put the constructor/named function inside any closure, it will mess with the execution order</span>
<span style="color: #003366; font-weight: bold;">function</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Object <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>.<span style="color: #660066;">constructor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
Array <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">constructor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts [object Array]</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">sort</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'c'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'b'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts a,b,c</span></pre></div></div>

<p>Here you are! I, however, have tested this piece of code in limited platforms and do not have the resources to test them. I know at least they work for grade-A browsers as published by YUI. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/07/29/javascript-kung-fu-object-array-and-literals/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
