<?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/tag/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>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>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>
