<?xml version="1.0" encoding="UTF-8"?>
<!-- name="generator" content="blojsom v3.2" -->
<rdf:RDF xmlns:wfw="http://wellformedweb.org/CommentAPI/"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         xmlns="http://purl.org/rss/1.0/">

	<channel rdf:about="http://dhtmlkitchen.com/news/default">
		<title>DHTML Kitchen News</title>
		<link>http://dhtmlkitchen.com/news/default/</link>
		<description>Front End Web Development</description>
		<dc:publisher>Garrett Smith</dc:publisher>
		<dc:creator>dhtmkitchen@gmail.com</dc:creator>
		<dc:date>2008-05-11T22:35:46-05:00</dc:date>
		<dc:language>en</dc:language>

        <items>
        <rdf:Seq>
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/05/11/Decorator-Factory-Aspect" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/05/06/The-JavaScript-Guru" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/05/05/APE-JavaScript-Library" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/04/19/Testing-JavaScript" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/02/21/Load-Time-Constants-Part-I" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/02/01/Google-Code-in-Safari" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/01/31/Crashed" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2008/01/04/Event-Notification-System" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2007/11/24/Browser-Detection" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2007/11/08/Opera-Bug-getComputedStyle-Returns-Margin-for-Unset-Top-Left-Values" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2007/10/21/Iteration-Enumeration-Primitives-and-Objects" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2007/10/18/Browser-Stats" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2007/10/10/How-to-Tell-if-an-Object-is-a-Function" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2007/10/05/How-Property-Access-Works" />
                                <rdf:li rdf:resource="http://dhtmlkitchen.com/news/default/2007/10/03/Opera-clobbers-object-prototype-with-FunctionExpression" />
                </rdf:Seq>
        </items>
    </channel>

            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/05/11/Decorator-Factory-Aspect">
	    <title>Factory Aspect, Added to a Decorator: APE.getById</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/05/11/Decorator-Factory-Aspect</link>
        <description>&lt;p&gt;
    This article describes the generic concepts for creating and mixing design patterns. 
    The basic principle for all design patterns is: &lt;em&gt;Encapsulate the parts that vary&lt;/em&gt;. 
&lt;/p&gt;

&lt;p&gt;
    The problem is finding a way to create a generic &lt;dfn&gt;Factory&lt;/dfn&gt; that can be reused on 
    various constructor functions for element &lt;dfn&gt;Decorators&lt;/dfn&gt;. This article explains 
    the problem and the process for finding the solution. 
&lt;/p&gt;

&lt;h3&gt;Decorator Factory Aspect&lt;/h3&gt;
&lt;p&gt;A &lt;dfn&gt;Decorator Factory Aspect&lt;/dfn&gt; is a 
&lt;dfn&gt;Factory method&lt;/dfn&gt;, added as an &lt;dfn&gt;Aspect&lt;/dfn&gt; to a constructor of a &lt;dfn&gt;Decorator&lt;/dfn&gt;.&lt;/p&gt;

&lt;p&gt;Before I explain how to add a &lt;dfn&gt;Factory&lt;/dfn&gt; to a constructor function for an element &lt;dfn&gt;decorator&lt;/dfn&gt;,
I should first define &lt;dfn&gt;Decorator&lt;/dfn&gt; (also called a &lt;dfn&gt;wrapper&lt;/dfn&gt;), &lt;dfn&gt;Factory&lt;/dfn&gt; and &lt;dfn&gt;Aspect&lt;/dfn&gt;. 
&lt;/p&gt;

&lt;dl&gt;
    &lt;dt&gt;Decorator Pattern&lt;/dt&gt;
    &lt;dd&gt;makes it possible to extend (decorate) the functionality of a class 
    by adding a new decorator class that wraps the original class. 
    (&lt;a
        href=&quot;http://en.wikipedia.org/wiki/Decorator_pattern&quot;&gt;Wikipedia link&lt;/a&gt;)
    &lt;/dd&gt;
    
    &lt;dt&gt;Factory Pattern&lt;/dt&gt;
    &lt;dd&gt;
        The Factory pattern is a creational design pattern 
        that encapsulates the processes of creating objects
    (&lt;a
        href=&quot;http://en.wikipedia.org/wiki/Factory_pattern&quot;&gt;Wikipedia link&lt;/a&gt;)
    &lt;/dd&gt;

    &lt;dt&gt;Aspect&lt;/dt&gt;
    &lt;dd&gt;
        &lt;cite&gt;&lt;q&gt;introduces separation of concerns, specifically cross-cutting concerns, as an advance in modularization&lt;/q&gt;&lt;/cite&gt; 
        (&lt;a
        href=&quot;http://en.wikipedia.org/wiki/Aspect-oriented_programming&quot;&gt;Wikipedia link&lt;/a&gt;)
    &lt;/dd&gt;
&lt;/dl&gt;

&lt;h4&gt;Decorator Examples&lt;/h4&gt;
&lt;p&gt;
  &lt;dfn&gt;Decorator&lt;/dfn&gt; is very common in JavaScript. For example: &lt;a href=&quot;http://developer.yahoo.com/yui/docs/Element.js.html&quot;&gt;YAHOO.util.Element&lt;/a&gt; &lt;dfn&gt;decorates&lt;/dfn&gt; an element, 
  jQuery &lt;dfn&gt;decorates&lt;/dfn&gt; an array of elements. 
&lt;/p&gt;

&lt;h4&gt;Factory Example&lt;/h4&gt;

&lt;p&gt;The &lt;dfn&gt;Factory&lt;/dfn&gt; gets or creates a &lt;dfn&gt;decorated&lt;/dfn&gt; element. 
The &lt;code&gt;id&lt;/code&gt; of the &lt;dfn&gt;wrapper&lt;/dfn&gt; is the same as the &lt;code&gt;id&lt;/code&gt; of the element. 
This is the part I want to make reusable:&lt;/p&gt;
&lt;pre&gt;
/**
 * @constructor
 * @param {String} id - the id of the element and widget. 
 */
function ElementWrapper(id, x) {
  this.id = id;
  this.x = x;
}

&lt;span class=&quot;comment&quot;&gt;// Factory. 
// &lt;span class=&quot;todo&quot;&gt;TODO:&lt;/span&gt; How can I make this generic/reusable?&lt;/span&gt;
ElementWrapper&lt;span style=&quot;color: #191; background: #FFFFc9&quot;&gt;.instances = {};&lt;/span&gt;
ElementWrapper&lt;span style=&quot;color: #191; background: #FFFFc9&quot;&gt;.getById = &lt;span class=&quot;keyword&quot;&gt;function&lt;/span&gt;(&lt;var&gt;id&lt;/var&gt;, &lt;var&gt;x&lt;/var&gt;) {
  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.instances.hasOwnProperty(&lt;var&gt;id&lt;/var&gt;)} &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.instances[&lt;var&gt;id&lt;/var&gt;];
  &lt;span class=&quot;keyword&quot;&gt;return this&lt;/span&gt;.instances[&lt;var&gt;id&lt;/var&gt;] = &lt;span class=&quot;keyword&quot;&gt;new this&lt;/span&gt;(&lt;var&gt;id&lt;/var&gt;, &lt;var&gt;x&lt;/var&gt;);
};&lt;/span&gt;

ElementWrapper.prototype = { 
  show : function() { 
    document.getElementById(this.id).style.visibility = &quot;visible&quot;;
  }
};
&lt;/pre&gt;

&lt;h5&gt;Benefits&lt;/h5&gt;
&lt;p&gt;
    Solves the problem of creating only one decorator per element &lt;code&gt;id&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
By calling &lt;code&gt;getElementById&lt;/code&gt;, the &lt;dfn&gt;decorator&lt;/dfn&gt; can avoid 
some of the problems with changing node references with &lt;code&gt;innerHTML&lt;/code&gt; (though state changes 
must still be managed manually).
&lt;/p&gt;

&lt;h3&gt;
  Problem: &lt;abbr title=&quot;Dont Repeat Yourself&quot;&gt;DRY&lt;/abbr&gt;
&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Don&#39;t Repeat Yourself&lt;/p&gt;
&lt;p&gt;
    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
  It is cumbersome and error-prone to write out a &lt;dfn&gt;Factory&lt;/dfn&gt; each time. 
Since this is an idiom I use a lot, it makes 
  sense to make it reusable.&lt;/p&gt;

&lt;p&gt;
    I want to have a generic &lt;code&gt;getById&lt;/code&gt; method that can be reused and will return 
    an instance of the constructor that it is called on. I want to be able to pass 
    extra arguments to that constructor (varargs). 
&lt;/p&gt;

&lt;h3&gt;Encapsulate the Parts That Vary&lt;/h3&gt;
&lt;p&gt;
&lt;strong&gt;What varies?&lt;/strong&gt; 
&lt;/p&gt;

&lt;p&gt;
  The &lt;code&gt;id&lt;/code&gt; parameter variable of &lt;code&gt;getById&lt;/code&gt; does not change; it will 
  always be present in any generic &lt;dfn&gt;Factory&lt;/dfn&gt;. 
  The parts of the &lt;dfn&gt;Factory&lt;/dfn&gt; that vary are: The additional zero or more arguments (varargs, 
  this case, &lt;code&gt;x&lt;/code&gt;),
  and the context, or &lt;dfn&gt;thisArg&lt;/dfn&gt;.
&lt;/p&gt;

&lt;p&gt;
    Resolving the context arg is easy. 
&lt;/p&gt;

&lt;p&gt;
  If I can solve passing varargs to a constructor in a generic context, 
  it will be possible to create a generic &lt;dfn&gt;Factory&lt;/dfn&gt; &lt;dfn&gt;Aspect&lt;/dfn&gt;. 
&lt;/p&gt;

&lt;h4&gt;Function newApply&lt;/h4&gt;
A way to call &lt;code&gt;new&lt;/code&gt; with variable arguments would solve this problem.
A &lt;code&gt;new + apply()&lt;/code&gt; would provide the varargs functionality of &lt;code&gt;apply&lt;/code&gt;, 
but passed to &lt;code&gt;[[Construct]]&lt;/code&gt;, not &lt;code&gt;[[Call]]&lt;/code&gt;. 

&lt;p&gt;This has been solved in &lt;a href=&quot;/ape&quot; title=&quot;AOP Pointcuts for JavaScript&quot;&gt;APE&lt;/a&gt; core. 
The source code for &lt;code&gt;APE.newApply&lt;/code&gt;:
&lt;/p&gt;
&lt;pre&gt;&lt;span class=&quot;comment&quot;&gt;
/** 
 * @param {Function} fun constructor to be invoked.
 * @param {Array} args arguments to pass to the constructor.
 * Instantiates a constructor and uses apply().
 */&lt;/span&gt;
newApply : &lt;span class=&quot;keyword&quot;&gt;function&lt;/span&gt;(&lt;var&gt;fun&lt;/var&gt;, &lt;var&gt;args&lt;/var&gt;) {
    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(arguments.length === 0) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt;;
    &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; &lt;var&gt;f&lt;/var&gt; = arguments.callee, &lt;var&gt;i&lt;/var&gt;;

    &lt;var&gt;f&lt;/var&gt;.prototype = &lt;var&gt;fun&lt;/var&gt;.prototype;// Copy prototype.
    &lt;var&gt;f&lt;/var&gt;.prototype.constructor = &lt;var&gt;fun&lt;/var&gt;;

    &lt;var&gt;i&lt;/var&gt; = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;var&gt;f&lt;/var&gt;;
    fun.apply(i, &lt;var&gt;args&lt;/var&gt;);  &lt;span class=&quot;comment&quot;&gt;// Apply the original constructor.&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;var&gt;i&lt;/var&gt;;
}
&lt;/pre&gt;

&lt;h4&gt;What&#39;s it Good For?&lt;/h4&gt;
&lt;p&gt;
  Now I can create the generic &lt;code&gt;getById&lt;/code&gt; function I wanted. This function can be added as an &lt;dfn&gt;aspect&lt;/dfn&gt; 
  to any constructor function. &lt;dfn&gt;Factory&lt;/dfn&gt; &lt;dfn&gt;Aspect&lt;/dfn&gt; &lt;code&gt;APE.getById&lt;/code&gt; is a part of 
  &lt;code title=&quot;AOP Pointcuts for EcmaScript&quot;&gt;APE&lt;/code&gt; core. 
&lt;/p&gt;

&lt;pre&gt;
getById : &lt;span class=&quot;keyword&quot;&gt;function&lt;/span&gt;(&lt;var&gt;id&lt;/var&gt;) {
    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.hasOwnProperty(&quot;instances&quot;)) &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.instances = {};
    &lt;span class=&quot;keyword&quot;&gt;return this&lt;/span&gt;.instances[&lt;var&gt;id&lt;/var&gt;] || (&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.instances[&lt;var&gt;id&lt;/var&gt;] = APE.newApply(&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;, arguments));       
},
&lt;/pre&gt;

&lt;h4&gt;Using the Generic &lt;code&gt;getById&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;This &lt;code&gt;getById&lt;/code&gt; method can be used with &lt;code&gt;ElementWrapper&lt;/code&gt; (above) 
or any other constructor that acts as a &lt;dfn&gt;Decorator&lt;/dfn&gt; to an element and 
accepts the element&#39;s &lt;code&gt;id&lt;/code&gt; as its first argument.
&lt;/p&gt;
&lt;pre&gt;
Slider = function(&lt;var&gt;id&lt;/var&gt;, &lt;var&gt;dir&lt;/var&gt;) { &lt;span class=&quot;comment&quot;&gt;/* ... */&lt;/span&gt; };

// Factory. 
&lt;span style=&quot;color:#191; background: #FFFFc9&quot;&gt;Slider.getById = APE.getById;&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
    Then I can use:
&lt;/p&gt;
&lt;pre&gt;
Slider.getById( &quot;weight&quot;, 1 );
&lt;/pre&gt;
&lt;p&gt;
Subsequent calls to:&lt;/p&gt;
&lt;pre&gt;
Slider.getById( &quot;weight&quot; );
&lt;/pre&gt;

&lt;p&gt;&amp;mdash; will return the same &lt;code&gt;Slider&lt;/code&gt; instance.&lt;/p&gt;

&lt;h3&gt;More Examples&lt;/h3&gt;

&lt;p&gt;I have used this approach for many parts of &lt;a title=&quot;AOP Pointcuts for EcmaScript&quot; href=&quot;/ape/&quot;&gt;APE&lt;/a&gt;, including &lt;a href=&quot;/ape/example/widget/calendar/&quot;&gt;Calendar&lt;/a&gt;,
&lt;a href=&quot;/ape/example/drag/&quot;&gt;Draggable, and Slider&lt;/a&gt;. It is most useful for building widgets.&lt;/p&gt;

&lt;h3&gt;Reusable Concept&lt;/h3&gt;
&lt;p&gt;Another closely related technique is &lt;dfn&gt;Decorator&lt;/dfn&gt; that accepts an &lt;em&gt;element&lt;/em&gt; instead 
of an element&#39;s &lt;code&gt;id&lt;/code&gt;. This is covered by &lt;code&gt;APE.getByNode&lt;/code&gt;.
&lt;/p&gt;

&lt;h3&gt;Source Code&lt;/h3&gt;
&lt;p&gt;
  &lt;a href=&quot;/ape/build/APE.js&quot; title=&quot;APE Library Core JavaScript&quot;&gt;APE.js&lt;/a&gt;
&lt;/p&gt;

&lt;h3&gt;Reflection&lt;/h3&gt;
&lt;p&gt;
In most patterns, encapsulating the parts that vary entails creating an class. 
However, in JavaScript, this particular pattern was simple to implement by using just 
two functions (&lt;code&gt;APE.getById&lt;/code&gt; and &lt;code&gt;APE.newApply&lt;/code&gt;) and leveraging the dynamic nature of 
JavaScript.
&lt;/p&gt;

&lt;h3&gt;Forward to ES4&lt;/h3&gt;
&lt;p&gt;
  ES4 has had some proposals for something called a &lt;dfn&gt;splat&lt;/dfn&gt; operator.
&lt;/p&gt;
&lt;pre&gt;
fun(...argsOrArray); 
new fun(...argsOrArray);
&lt;/pre&gt;

&lt;p&gt;This proposal will allow passing varargs to a constructor or a function call.&lt;/p&gt;

&lt;p&gt;It is unclear how the &lt;dfn&gt;splat&lt;/dfn&gt; operator, if accepted into the language, will work with 
functions which expect typed arguments, in strict mode. 
&lt;/p&gt;

&lt;h3&gt;Links&lt;/h3&gt;
&lt;p&gt;
&lt;a href=&quot;http://www.artima.com/intv/dry.html&quot;&gt;Orthogonality and the DRY Principle&lt;/a&gt;,
A Conversation with Andy Hunt and Dave Thomas, Part II
by Bill Venners
March 10, 2003
&lt;/p&gt;
</description>
	    <dc:date>2008-05-11T22:35:46-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/JavaScript/2008/05/11/Decorator-Factory-Aspect</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2008/05/11/Decorator-Factory-Aspect?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/05/06/The-JavaScript-Guru">
	    <title>The JavaScript Guru</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/05/06/The-JavaScript-Guru</link>
        <description>&lt;p&gt;
Programming is not &quot;wizardry.&quot; I am not a &quot;ninja&quot; or a &quot;guru.&quot; 
&lt;/p&gt;

&lt;h3&gt;What, no Sword?&lt;/h3&gt;
&lt;p&gt;
Programming is a scientific discipline. It requires zero mystical powers (although I have been known to &quot;disappear&quot; before long meetings). It is hard work. If you want a guru, go climb a mountain (results not guaranteed). 
&lt;/p&gt;

&lt;h3&gt;More Yellow Flags&lt;/h3&gt;
&lt;p&gt;
I sometimes get job descriptions and do interviews that can put me off. &quot;JavaScript Ninja&quot; is one flag.
&lt;/p&gt;

&lt;p&gt;
&quot;We want someone who can &lt;em&gt;Hit the Ground Running and Work Independently&lt;/em&gt;,&quot; is another more subtle flag.
&lt;/p&gt;

&lt;p&gt;
  This usually means they want someone to work alone, not with the team.
&lt;/p&gt;

&lt;h3&gt;They Don&#39;t Want what&#39;s Good for Them&lt;/h3&gt;
&lt;p&gt;
Part of what I&#39;m finding to be a problem is that employers get the impression that I&#39;m good at DHTML and they think they&#39;ll have me work by myself. 
&lt;/p&gt;

&lt;p&gt;
The situation where one or more members on the team are unwilling put forth the mental effort to do what needs to get done can lead to hiring a contractor. This is a terrible position for any contractor to be in.

&lt;/p&gt;

&lt;h3&gt;Teamwork and Ownership&lt;/h3&gt;

&lt;p&gt;
The reality is that the more successful projects have better cooperation and communication.
Hiring a &quot;guru&quot; or &quot;wizard&quot; contractor is usually carried out for the
worst reasons (&quot;we&#39;re behind&quot;), and with the worst process (&quot;let&#39;s
have the contractor own this part&quot;). Hiring new people takes time. Even a contractor. It
also takes time integrating the new person. So hiring a contractor to &quot;catch up&quot; isn&#39;t generally a good idea.
&lt;/p&gt;

&lt;p&gt;
Having code in the project that is owned by the contractor gets the 
company in technical debt because it leaves them with code that they do not understand. 
&lt;/p&gt;

&lt;p&gt;
Having the contractor sit and work apart from the team often makes it much harder for the contractor to do a proper job. 
&lt;/p&gt;

&lt;h3&gt;Change&lt;/h3&gt;
&lt;p&gt;
When [x_new_feature] is introduced, it might require change to the existing code. If it&#39;s a new feature, then it often means that the requirements changed in some way. Often the existing code is not designed to accommodate the new change (normal). If the contractor is asked to build [x_new_feature], and he is working alone, well, then he either has to step outside his role and change existing code (can get him fired) or the new feature will get built in a hackish way around the other non-changing code that is owned by the team. 
&lt;/p&gt;

&lt;p&gt;
Hiring a contractor is perfectly fine, but having contractor-owned code makes it hard for the team to handle changes, leads to code rot, and sometimes leads to the company hiring
another contractor. This is wasteful (no matter how many millions of investor money is available). It is beneficial for the company
employees to understand as much of the company code as possible. A
contractor should not be a lone gun, but should be working with the
team, trying to understand their problem with them, collaboratively, and helping
them to find solutions to the problems.
&lt;/p&gt;

&lt;h3&gt;Hire a Ninja?&lt;/h3&gt;
&lt;p&gt;
I am very cautious of anyone who presents himself as a &quot;ninja&quot; or a &quot;guru&quot;. Posting an ad for &quot;JavaScript Guru Wanted,&quot; is asking for someone who is both overconfident and underqualified, and that is disastrous combination of characteristics in a potential newhire. 
&lt;/p&gt;

&lt;p&gt;
The code belongs to the company and gets written by the
team. Having the contractor work apart from the team on a side project or sit in a different area is a bad idea; it just gets the company into debt more.
&lt;/p&gt;

&lt;p&gt;
Pair programming, metaphor/ubiquitous language, daily standups should include the contractor. With complicated applications, TDD and Pair can be practiced in a way that can significantly benefit the successful outcome of the project and collective understanding of the code. Even with a contractor. 

&lt;/p&gt;

&lt;p&gt;
 Let the contractor know how your team works and how he&#39;s expected to fit in. This will help avoid misunderstandings and can help your team find the right contractor for the job. 
&lt;/p&gt;
</description>
	    <dc:date>2008-05-06T20:15:04-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/uncategorized/2008/05/06/The-JavaScript-Guru</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2008/05/06/The-JavaScript-Guru?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/05/05/APE-JavaScript-Library">
	    <title>APE JavaScript Library</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/05/05/APE-JavaScript-Library</link>
        <description>&lt;p&gt;Frustrated with every other Javascript framework, I have decided to write something better.
&lt;/p&gt;

&lt;h3&gt;Points&lt;/h3&gt;
&lt;ul&gt;
 &lt;li&gt; Minimal Framework, mainly used for AOP and OOP functionality &lt;/li&gt;
 &lt;li&gt; All code is tested using YUI Test (TDD) &lt;/li&gt;
 &lt;li&gt; AOP Event System with asynchronous error handling &lt;/li&gt;
 &lt;li&gt; Namespacing &lt;/li&gt;
 &lt;li&gt; Modularity, Cohesion, Packaging &lt;/li&gt;
 &lt;li&gt; No browser detection &lt;/li&gt;
 &lt;li&gt; JSDoc &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here it is: &lt;a href=&quot;/ape/&quot;&gt;APE JavaScript Library&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Testing&lt;/h3&gt;
&lt;p&gt;
By using a test driven approach, I was able to avoid problems that I found in other libraries. 
&lt;/p&gt;

&lt;h4&gt;APE Core&lt;/h4&gt;
&lt;p&gt;
  The core of APE is so small that the overhead of adding APE to a project is significantly less than that of other libraries. 
&lt;/p&gt;

&lt;p&gt;
 I generally don&#39;t like a lot of the libraries that use a &lt;a href=&quot;http://en.wikipedia.org/wiki/God_object&quot;&gt;&quot;GOD&quot; object&lt;/a&gt; approach. These objects can be recognized by being undefinable by more than a few words. The approach is quite popular and annoys me to no end. How do you describe the &lt;code&gt;dojo&lt;/code&gt; object, for example?
&lt;/p&gt;

&lt;p&gt;
 The &lt;code&gt;APE&lt;/code&gt; object itself is defined in APE.js, providing only the most basic things that a framework would need: 1) Object creational features, and 2) a core namespace. Although some might argue that a library core needs more than that. 
&lt;/p&gt;

&lt;p&gt;
The APE object is only &lt;a href=&quot;/ape/build/APE.js&quot;&gt;&lt;abbr title=&quot;Three Kilobytes&quot;&gt;3k&lt;/abbr&gt; minified&lt;/a&gt; and less than &lt;abbr title=&quot;One Kilobyte&quot;&gt;1k&lt;/abbr&gt; gzipped. The size is not the point. The point is that APE is not a kitchen-sink, or God object.
&lt;/p&gt;


&lt;h3&gt;Help Wanted&lt;/h3&gt;
&lt;p&gt;
 You can join, too! Here&#39;s what APE needs:
&lt;/p&gt;

&lt;h4&gt;New Features&lt;/h4&gt;
&lt;p&gt;
 There is much more that can be added. For example, some ADT&#39;s: Resizable (croppers, windows), FormSerializer.
&lt;/p&gt;
&lt;h4&gt;SVN and Bugzilla&lt;/h4&gt;
&lt;p&gt;
 I still need to add hosted SVN to the directories and install bugzilla.
&lt;/p&gt;

&lt;h4&gt;Performance Tests&lt;/h4&gt;
&lt;p&gt;
  I&#39;ve always been big on performance, and not just for JavaScript but all areas of life, and I am very competitive in nature. 
&lt;/p&gt;

&lt;p&gt;
APE needs benchmarks to compare with other libraries. This will provide useful analysis for other library authors who aspire to have code that is as performant as that in APE.
&lt;/p&gt;

&lt;p&gt;
 Anyone interested in joining this project contact dhtmlkitchen - at - gmail - dot - com. 
&lt;/p&gt;
</description>
	    <dc:date>2008-05-05T19:26:12-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/uncategorized/2008/05/05/APE-JavaScript-Library</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2008/05/05/APE-JavaScript-Library?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/04/19/Testing-JavaScript">
	    <title>Testing JavaScript</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/04/19/Testing-JavaScript</link>
        <description>&lt;p&gt;Slides from JS Meetup&#39;s &quot;Testing JavaScript&quot;
&lt;/p&gt;
&lt;div style=&quot;width:425px;text-align:left;margin:auto&quot; id=&quot;__ss_361183&quot;&gt;&lt;object style=&quot;margin:0px&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://static.slideshare.net/swf/ssplayer2.swf?doc=testing-1208582883379454-8&quot;/&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;/&gt;&lt;param name=&quot;allowScriptAccess&quot; value=&quot;always&quot;/&gt;&lt;embed src=&quot;http://static.slideshare.net/swf/ssplayer2.swf?doc=testing-1208582883379454-8&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div style=&quot;font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;&quot;&gt;&lt;a href=&quot;http://www.slideshare.net/?src=embed&quot;&gt;&lt;img src=&quot;http://static.slideshare.net/swf/logo_embd.png&quot; style=&quot;border:0px none;margin-bottom:-5px&quot; alt=&quot;SlideShare&quot;/&gt;&lt;/a&gt; | &lt;a href=&quot;http://www.slideshare.net/dhtml/testing-361183?src=embed&quot; title=&quot;View &#39;Testing&#39; on SlideShare&quot;&gt;View&lt;/a&gt; | &lt;a href=&quot;http://www.slideshare.net/upload?src=embed&quot;&gt;Upload your own&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;
</description>
	    <dc:date>2008-04-19T01:03:24-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/JavaScript/2008/04/19/Testing-JavaScript</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2008/04/19/Testing-JavaScript?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/02/21/Load-Time-Constants-Part-I">
	    <title>Load Time Constants, Part I</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/02/21/Load-Time-Constants-Part-I</link>
        <description>&lt;p&gt;
One issue that comes up when writing scripts is addressing upper and lower case tag names. 
&lt;/p&gt;
&lt;h3&gt;Problem&lt;/h3&gt;
&lt;p&gt;
An XHTML document served with the content type &lt;code&gt;application/xhtml+xml&lt;/code&gt; will have all lower case tag names in the &lt;abbr title=&quot;Document Object Model&quot;&gt;DOM&lt;/abbr&gt;.
The exact same page served with the content type &lt;code&gt;text/html&lt;/code&gt; will have all upper case tag names.
&lt;/p&gt;

&lt;p&gt;
This can affect the way we script our pages by requiring us to clutter up the script with calls to &lt;code&gt;toLowerCase()&lt;/code&gt;.
&lt;/p&gt;

&lt;h3&gt;
Example
&lt;/h3&gt;
&lt;pre&gt;
function getCoords(el) {
  for( var parent = el.parentNode; parent &amp;amp;&amp;amp; parent !== container; 
                                         parent = parent.parentNode) { 
    if( parent.tagName&lt;strong style=&quot;color:red&quot;&gt;.toLowerCase()&lt;/strong&gt; == &quot;table&quot; ) {
      var pcs = getComputedStyle(parent, &quot;&quot;);
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;
It&#39;s easy enough to call &lt;code&gt;el.tagName.toLowerCase()&lt;/code&gt; but when this is done repeatedly in a loop, it can slow things down. 
&lt;/p&gt;

&lt;h3&gt;Solution&lt;/h3&gt;
&lt;p&gt;
One way to avoid that issue is to define a run time constant. This would be done in a closure over the function that was calling &lt;code&gt;toLowerCase()&lt;/code&gt;
&lt;/p&gt;
&lt;pre&gt;
(function(){ 

var TABLE = /^h/.test(document.documentElement.tagName) ? &quot;table&quot; : &quot;TABLE&quot;;

function getCoords(el) {
  for( var parent = el.parentNode; parent &amp;amp;&amp;amp; parent !== container; 
                                         parent = parent.parentNode) { 
    if( &lt;strong style=&quot;color: #060&quot;&gt;parent.tagName == TABLE&lt;/strong&gt; ) {
      var pcs = getComputedStyle(parent, &quot;&quot;);
    }
  }
}
})();
&lt;/pre&gt;
&lt;h3&gt;Scope&lt;/h3&gt;
&lt;p&gt;
  Function &lt;code&gt;getCoords()&lt;/code&gt; can see &lt;code&gt;TABLE&lt;/code&gt;, but nothing outside of that closure can see &lt;code&gt;getCoords&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Function &lt;code&gt;getCoords&lt;/code&gt; must then be exported by assigning it to a property of a globally accessible object (using the Module Pattern or Exporting Pattern).
&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;
Having a constant can help to speed up the loop by avoiding the property lookup and 
an extra function call to &lt;code&gt;toLowerCase()&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Having one constant value reduces the number of strings being created and garbage collected. Since 
an active garbage collector can also hurt performance, a constant value avoids this hit.
&lt;/p&gt;

&lt;p&gt;
Finally, the result can be compressed using a compression tool, resulting in &lt;code&gt;TABLE&lt;/code&gt; being converted in to a one letter symbol.
&lt;/p&gt;
</description>
	    <dc:date>2008-02-21T17:20:24-06:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/JavaScript/2008/02/21/Load-Time-Constants-Part-I</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2008/02/21/Load-Time-Constants-Part-I?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/02/01/Google-Code-in-Safari">
	    <title>Google Code in Safari</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/02/01/Google-Code-in-Safari</link>
        <description>&lt;p&gt;
 The JavaScript Gurus at google have developed a neat feature for Googlecode. It verifies the form for you, to make sure you&#39;re data is OK. If their script thinks you&#39;ve correctly filled out the form, the submit button gets enabled.
&lt;/p&gt;

&lt;p&gt;
 This has got to be one of the dumbest things I&#39;ve seen in a long time...
&lt;/p&gt;

&lt;img src=&quot;/img/blog/googlecode.jpg&quot; alt=&quot;Google Code: Borken in WebKit&quot;/&gt;

&lt;p&gt;
 See that &lt;samp&gt;Create Project&lt;/samp&gt; button up there?
&lt;/p&gt;

&lt;p&gt;
It&#39;s disabled, isn&#39;t it? Yes, it is. And if you&#39;re using Webkit, it will stay disabled. 
&lt;/p&gt;

&lt;h3&gt;User Experience and Usability&lt;/h3&gt;
&lt;p&gt;
 There is JavaScript code in Google&#39;s &quot;create project&quot; page that enables the button. Since it doesn&#39;t work in Webkit, the website is unusable.
&lt;/p&gt;

&lt;h3&gt;Surprised?&lt;/h3&gt;
&lt;p&gt;
 It&#39;s nothing to be surprised about. The new GMail is just as buggy in Safari as the old one was; sometimes the message gets only half-sent (truncation). Google Groups does the same thing; sends half a message sometimes (embarrassingly horrible code there). Groups&#39; &quot;search this group&quot; feature doesn&#39;t work in any browser if you have language pref (&lt;a href=&quot;http://groups.google.com/group/comp.lang.javascript/topics&quot;&gt;link&lt;/a&gt;). Groups also has the same problem with the button becoming disabled when it is clicked, which doesn&#39;t work so well on a wifi remote connection if you got disconnectet. It really is a piece of garbage. 
&lt;/p&gt;

&lt;h3&gt;A Reputation to Live up to&lt;/h3&gt;
&lt;p&gt;
Arrogantly bolstered by aggressive financial growth, the Google Empire churns out one poorly engineered software project after another. Each project gaining massive recognition, as it bears the Google brand name. Like Microsoft, they&#39;re accepted as de facto in the industry; (1) Like Microsoft, they aggressively hire engineers, acquire competitors, et c. (2)
&lt;/p&gt;

&lt;p&gt;
(1) Got an iPhone? I bet it came with Google&#39;s lame mobile search instead of the better Yahoo Mobile search.
&lt;/p&gt;

&lt;p&gt;
(2)
Google docs and spreadsheets, an acquisition, is developed and maintained by recent grads and contractors. No surprise that even after nearly 4 years it&#39;s still almost completely unusable. 
&lt;/p&gt;

&lt;h3&gt;Source Code&lt;/h3&gt;
&lt;p&gt;
The inline &amp;lt;script&amp;gt; tag for &lt;a href=&quot;http://code.google.com/hosting/createProject.do&quot;&gt;Google Code&lt;/a&gt; contains the relevant button-enabling code. No type attribute on the script tag, missing &lt;code&gt;var&lt;/code&gt; keyword for most of the variables (which are all top level). The obvious point is the accessibility issue: Users who don&#39;t have a browser Google supports are blocked.
&lt;/p&gt;

&lt;pre&gt;
&amp;lt;script&amp;gt;
 _exposeExistingLabelFields(&#39;edit&#39;);
 var submit = document.getElementById(&#39;submit&#39;);
 submit.disabled=&#39;disabled&#39;;
 var projectname = document.getElementById(&#39;projectname&#39;);
 var licensekey = document.getElementById(&#39;license_key&#39;);
 var summary = document.getElementById(&#39;summary&#39;);
 var description = document.getElementById(&#39;description&#39;);
 var cg = document.getElementById(&#39;cg&#39;);
 projectname.focus();
 var solelyDigits = /^[-0-9]+$/
 var hasUppercase = /[A-Z]/
 var projectRE = /^[a-z0-9][-a-z0-9]*$/
 function checkprojectname() {
 &lt;b style=&quot;color:#c00&quot;&gt;name = projectname.value;&lt;/b&gt;
 &lt;b style=&quot;color:#c00&quot;&gt;feedback = document.getElementById(&#39;projectnamefeedback&#39;);&lt;/b&gt;
 &lt;b style=&quot;color:#c00&quot;&gt;submit.disabled=&#39;disabled&#39;;&lt;/b&gt;
 feedback.style.color = &#39;red&#39;;
 if (name == &#39;&#39;) {
 feedback.innerHTML = &#39;&#39;;
 } else if (hasUppercase.test(name)) {
 feedback.innerHTML = &#39;Must be all lowercase&#39;;
 } else if (solelyDigits.test(name)) {
 feedback.innerHTML = &#39;Must include a lowercase letter&#39;;
 } else if (!projectRE.test(name)) {
 feedback.innerHTML = &#39;Invalid project name&#39;;
 } else if (name.length &amp;gt; 50) {
 feedback.innerHTML = &#39;Project name is too long&#39;;
 } else {
 feedback.innerHTML = &#39;&#39;;
 feedback.style.color = &#39;&#39;;
 
 checksubmit()
 }
 }
 function checksubmit() {
 submit.disabled=&#39;disabled&#39;;
 if (projectname.value.length &amp;gt; 0 &amp;amp;&amp;amp;
 licensekey.value.length &amp;gt; 1 &amp;amp;&amp;amp; 
 summary.value.length &amp;gt; 3 &amp;amp;&amp;amp; 
 description.value.length &amp;gt; 3 &amp;amp;&amp;amp; 
 (cg == undefined || cg.value.length &amp;gt; 1)) {
 submit.disabled=&#39;&#39;;
 }
 }
 checkprojectname();
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
&lt;h4&gt;Looking at the Mess&lt;/h4&gt;
&lt;p&gt;
Notice the:
&lt;code&gt;submit.disabled=&#39;disabled&#39;;&lt;/code&gt;, well that&#39;s pretty odd. I can guess what the author was trying to do. The browser might do a boolean conversion on the &#39;disabled&#39;, to get a value of &lt;code&gt;true&lt;/code&gt;, but that is not guaranteed. I&#39;m guessing there&#39;s code somewhere else that says &lt;code&gt;submit.disabled = &quot;&quot;&lt;/code&gt; or &lt;code&gt;submit.removeAttibute(&quot;disabled&quot;)&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
 I love how they embed all the localized error messages in a series &lt;code&gt;if else&lt;/code&gt; of &lt;code&gt;checkprojectname&lt;/code&gt; function. A fine example of Google&#39;s standard of quality.
&lt;/p&gt;

&lt;h3&gt;What I Think of Google&#39;s Code&lt;/h3&gt;
&lt;p&gt;
 Garbage. I wouldn&#39;t care so much if their god damned page worked.
&lt;/p&gt;
</description>
	    <dc:date>2008-02-01T02:17:05-06:00</dc:date>
	        </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/01/31/Crashed">
	    <title>Crashed!</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/01/31/Crashed</link>
        <description>&lt;p&gt;
DHTML Kitchen was down.
&lt;/p&gt;


&lt;p&gt;
My web host upgraded CPanel. They did not tell me, but they did. In so
doing, the permissions on my database got changed, so my blog software could not access my MySQL database. The blog software threw an error, which crashed 
Tomcat. Tomcat could not be restarted, because when the app loaded, the blog software would try to read from the database, which would cause another crash, bringing Tomcat right down again.
&lt;/p&gt;

&lt;p&gt;
Glad it&#39;s back up and running now.
&lt;/p&gt;
</description>
	    <dc:date>2008-01-31T02:25:05-06:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/DHTML+Kitchen/2008/01/31/Crashed</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2008/01/31/Crashed?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2008/01/04/Event-Notification-System">
	    <title>Event Notification System</title>
	    <link>http://dhtmlkitchen.com/news/default/2008/01/04/Event-Notification-System</link>
        <description>            &lt;p&gt;
    An &lt;dfn&gt;Event Notification System&lt;/dfn&gt; is an object that manages notification of events to multiple callbacks. 
    The &lt;dfn&gt;Event Notification System&lt;/dfn&gt; uses an &lt;dfn&gt;Event Registry&lt;/dfn&gt; to store the callbacks as bound methods. When the event 
    fires, the callbacks are invoked.

&lt;/p&gt;

&lt;h3&gt;Event Registry&lt;/h3&gt;
&lt;p&gt;
    An &lt;dfn&gt;Event Registry&lt;/dfn&gt; is a store of bound methods. 
    An &lt;dfn&gt;Event Registry&lt;/dfn&gt; is used by an &lt;dfn&gt;Event Notification System&lt;/dfn&gt;. 
    The &lt;dfn&gt;Event Notification System&lt;/dfn&gt; is tightly coupled with the &lt;dfn&gt;Event Registry&lt;/dfn&gt;. Sometimes it is referred to &lt;em&gt;as&lt;/em&gt; the &lt;dfn&gt;Registry&lt;/dfn&gt;.
    In reality, the &lt;dfn&gt;Registry&lt;/dfn&gt; is just a data structure and the &lt;dfn&gt;Event Notification System&lt;/dfn&gt; is a behavioral object.

&lt;/p&gt;

&lt;p&gt;Almost Every JavaScript library has an &lt;dfn&gt;Event Registry&lt;/dfn&gt;, or at least some way of dealing with event notification. 
&lt;/p&gt;

&lt;h4&gt;For example:&lt;/h4&gt;
&lt;pre&gt;// YUI:
YAHOO.util.Event.addListener( link, &quot;click&quot;, linkClickHandler, thisArg );

// Prototype: &lt;span style=&quot;color: rgb(153, 0, 0);&quot;&gt;(not a registry, but the old &#39;addEvent&#39; function renamed)&lt;/span&gt;.
Event.observe( link, &quot;click&quot;, linkClickHandler ); 

// Dojo:
dojo.connect( link, &quot;onclick&quot;, window, &quot;linkClickHandler&quot; );
&lt;/pre&gt;

&lt;p&gt;
    They&#39;re all different in how they work. 

&lt;/p&gt;

&lt;p&gt;
    The &lt;dfn&gt;Event Registry&lt;/dfn&gt; is useful for a few reasons. 
&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;It allows multiple callbacks to be assigned to a function call.&lt;/li&gt;
    &lt;li&gt;Provides a usable alternative to &lt;code&gt;attachEvent&lt;/code&gt;. Internet Explorer 7 and below has &lt;code&gt;attachEvent&lt;/code&gt;/&lt;code&gt;detachEvent&lt;/code&gt;. The callback function for &lt;code&gt;attachEvent&lt;/code&gt; executes in global 
    context (&lt;code&gt;this&lt;/code&gt; is &lt;code&gt;window&lt;/code&gt;), not the object it was attached to.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;
    A good &lt;dfn&gt;Event Registry&lt;/dfn&gt; solves these problems. A good &lt;dfn&gt;Event Registry&lt;/dfn&gt; also allows for context resolution with an optional 
    &lt;code&gt;thisArg&lt;/code&gt;. A good &lt;dfn&gt;Event Registry&lt;/dfn&gt; also allows custom events to be registered using the same interface.
&lt;/p&gt;

&lt;p&gt;
    A poorly designed &lt;dfn&gt;Event Registry&lt;/dfn&gt; concerns itself with things related to native events (&lt;code&gt;DOMContentLoaded&lt;/code&gt;, &lt;code&gt;keyPress&lt;/code&gt;, et c). A poorly designed 
    Event Registry does not pass an event object to the callback (perhaps trying to use &lt;code&gt;eval&lt;/code&gt; to pass varargs). 
&lt;/p&gt;

&lt;h3&gt;Error Handling in an &lt;dfn&gt;Event Notification System&lt;/dfn&gt;&lt;/h3&gt;

&lt;h4&gt;Callback Errors Should not Break the Registry&lt;/h4&gt;
&lt;p&gt;A good &lt;dfn&gt;Event Registry&lt;/dfn&gt; does not allow any callback to break the registry.
&lt;/p&gt;

&lt;p&gt;
    One common problem in most &lt;dfn&gt;Event Notification System&lt;/dfn&gt;s (such as Dojo, Mochikit, YUI, and jQuery) is that they allow the callback to break the System. If a callback fails, it prevents subsequent callbacks from firing. A callback should not be given the ability to break the &lt;dfn&gt;Registry&lt;/dfn&gt;. 
&lt;/p&gt;

&lt;p&gt;
    Here&#39;s how to break a &lt;dfn&gt;Registry&lt;/dfn&gt; that doesn&#39;t consider errors:
&lt;/p&gt;
&lt;pre&gt;var passed = false;
addCallback( link, &quot;click&quot;, function(){ setTimeout(checkTitle, 500); } );
addCallback( link, &quot;click&quot;, function(){ throw Error(&#39;bad&#39;); } );
addCallback( link, &quot;click&quot;, function(){ passed = true; } );

function checkTitle(){ 
    if(!passed) 
        alert(&quot;registry broken: second callback did not fire.&quot;);
    else 
        alert(&#39;passed&#39;);
}
&lt;/pre&gt;

&lt;p&gt;
    Callbacks sometimes throw Errors. It is important for the &lt;dfn&gt;Event Registry&lt;/dfn&gt; to consider this and take the responsibility to handle these errors properly. If an error occurs in a callback, it should not break the &lt;dfn&gt;Registry&lt;/dfn&gt;. 

&lt;/p&gt;

&lt;p&gt;
    It should be guaranteed that all callbacks fire, even when earlier callbacks throw errors. This is a natural 
    expectation; it&#39;s exactly how DOM Events work:
&lt;/p&gt;

&lt;h4&gt;DOM Events Test&lt;/h4&gt;
&lt;div&gt;
&lt;pre id=&quot;registry-dom-event-test&quot;&gt;(function(){
var s = document.getElementById(&#39;r-test&#39;);

var el = document.getElementById(&quot;registry-dom-event-button&quot;);
if(!window.hasDocumentListeners) {
    el.addEventListener( &quot;click&quot;, setUpCheck, false );
    el.addEventListener(&quot;click&quot;, throwError, false );
    // setTitle must fire.
    el.addEventListener( &quot;click&quot;, setTitle, false ); 

    window.hasDocumentListeners = true;
}
function setUpCheck(){ setTimeout(checkTitle, 500); }
function throwError(){ document.title = &quot;&quot;; throw Error(&#39;bad&#39;); }
function setTitle(){ document.title += &#39;ok&#39;; }
function checkTitle(ev) {
    if(document.title != &quot;ok&quot;) {
        alert(&quot;DOM Events broken: setTitle did not fire. &quot; + document.title );
    }
    else {
        alert(&quot;passed&quot;);
    }
}
})();&lt;/pre&gt;
&lt;button id=&quot;registry-dom-event-button&quot; onfocus=&quot;eval(document.getElementById(&#39;registry-dom-event-test&#39;).textContent)&quot;&gt;eval&lt;/button&gt;
&lt;/div&gt;
&lt;h4&gt;Result and Analysis&lt;/h4&gt;

&lt;p&gt;There should be 1 error and an alert &lt;kbd&gt;passed&lt;/kbd&gt;. This indicates that after the error happened, the &lt;code&gt;setTitle&lt;/code&gt; callback successfully fired.
&lt;/p&gt;

&lt;p&gt;
    This example assumes:
&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt; Callbacks fire in the order in which they were registered. &lt;/li&gt;

    &lt;li&gt; The &lt;kbd&gt;bad&lt;/kbd&gt; error in the first callback does not stop subsequent callbacks from firing. &lt;/li&gt;
    &lt;li&gt; The button supports the &lt;a href=&quot;http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Registration-interfaces&quot;&gt;&lt;code&gt;EventTarget&lt;/code&gt; interface&lt;/a&gt; (not in IE7).
&lt;/li&gt;
&lt;li&gt; The &lt;code&gt;button&lt;/code&gt; supports onfocus (not in safari &lt;a href=&quot;http://bugs.webkit.org/show_bug.cgi?id=16331&quot;&gt;bug 16331&lt;/a&gt;).

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Proper Callback Error-Handling&lt;/h3&gt;
&lt;p&gt;
    Throwing the error in a separate thread allows the callstack to continue without breaking. Any errors that are thrown 
    are thrown in the correct order in the callstack. The &lt;code&gt;Event Publisher&lt;/code&gt;&#39;s &lt;code&gt;fire&lt;/code&gt; 
    function would have something like this:
&lt;/p&gt;

&lt;pre&gt;try {
// If an error occurs, continue the event fire,
// but still throw the error.
  callback.call( thisArg, ev );
}
catch( ex ) {
  setTimeout(&quot;throw ex;&quot;, 1); 
}
&lt;/pre&gt;

&lt;p&gt;
  The one subtle issue is that &lt;code&gt;setTimeout&lt;/code&gt; uses global scope, like the Function constructor, not like eval, which runs in the calling context&#39;s scope. 
&lt;/p&gt;

&lt;p&gt;
  A closure must be used to preserve the &lt;code&gt;ex&lt;/code&gt; variable.
&lt;/p&gt;

&lt;pre&gt;try {
// If an error occurs, continue the event fire,
// but still throw the error.
  callback.call( thisArg, ev );
}
catch( ex ) {
  setTimeout(function(){ throw ex; }, 1); 
}
&lt;/pre&gt;


&lt;h3&gt;Event Registry Test&lt;/h3&gt;
&lt;p&gt;
  The remaining problem with the above code is that the error condition is untestable. Writing a test suite forced me to realize this 
  and I changed the design.
&lt;/p&gt;

&lt;pre&gt;try {
if(csi[0].call(csi[1], e) == false)
  preventDefault = true; // continue main callstack and return false afterwards.
}
catch(ex) {
  APE.deferError(ex);
}
&lt;/pre&gt;

&lt;p&gt;
  Where &lt;code&gt;APE.deferError&lt;/code&gt; is defined:

&lt;/p&gt;

&lt;pre&gt;deferError : function(error) {		
  setTimeout(function deferError(){throw error;},1);
}
&lt;/pre&gt;

&lt;p&gt;
    I have included the source code for my own &lt;dfn&gt;Event Registry&lt;/dfn&gt;, along with this 
    &lt;a href=&quot;/ape/test/tests/EventPublisher-test.html&quot; onclick=&quot;window.open(this.href);return false;&quot;&gt;test&lt;/a&gt;,
    which shows how I managed to test &lt;code&gt;APE.deferError&lt;/code&gt;.
&lt;/p&gt;

&lt;h3&gt;Performance?&lt;/h3&gt;

&lt;p&gt;
    Wrapping each callback call in a &lt;code&gt;try catch&lt;/code&gt; might seem to be bad for performance. 
    I tried it with mousemove event on my drag code, dragging multiple drag objects at a time 
    (&lt;a href=&quot;/ape/example/drag/droptarget/&quot;&gt;example&lt;/a&gt;), and it seemed fast enough; I did not notice performance 
    problems in any browser. There is most likely some performance overhead using this approach,
    but I did not find a need to write a benchmark. 
&lt;/p&gt;

&lt;div class=&quot;gotcha&quot;&gt;
    &lt;p&gt;
        &lt;code&gt;src&lt;/code&gt; should never be a string. Although this may seem obvious, YUI actually allows &lt;code&gt;src&lt;/code&gt; to be a string, 
        where the string represents an element&#39;s ID. The document is polled regulary until 
        the element with the &lt;code&gt;id&lt;/code&gt; matching string is found and then the callback is attached to that element. If the element has been renamed, the document is still polled and silent failure occurs.
    &lt;/p&gt;

    &lt;p&gt;
        This can lead to silent failure or corrupted application state if the element is not found. It is not recommended. 
    &lt;/p&gt;
&lt;/div&gt;

&lt;h3&gt;Packaging and API Design&lt;/h3&gt;
&lt;p&gt;
    The &lt;dfn&gt;Event Notification System&lt;/dfn&gt; is a low level component with no external dependencies.
&lt;/p&gt;

&lt;p&gt;
    Being a low level component, the &lt;dfn&gt;Event Notification System&lt;/dfn&gt; should be maximally stable (no efferent couplings), and maximally abstract. In this case, the Event Notification system is maximally abstract because 
    it can&#39;t be subclassed or used independently. 
&lt;/p&gt;

&lt;h3&gt;Stable Dependencies Principle&lt;/h3&gt;
&lt;p&gt;Depend in the direction of stability&lt;/p&gt;

&lt;h3&gt;Stable Abstractions Principle&lt;/h3&gt;
&lt;p&gt;

    A package should be as abstract as it is stable.
&lt;/p&gt;

&lt;h3&gt;Reuse Equivalence Principle&lt;/h3&gt;
&lt;p&gt;
    The Granule of Reuse is the Granule of Release.
&lt;/p&gt;

&lt;p&gt;
    The &lt;dfn&gt;Event Notification System&lt;/dfn&gt; is a low level component with no external dependencies. It is intentionally 
    packaged as a single, tested unit. It amplifies the essential (event notification) and eliminates the irrelevant.

&lt;/p&gt;

&lt;p&gt;
    Creating special cases for handling &lt;acronym title=&quot;Document Object Model&quot;&gt;DOM&lt;/acronym&gt; events (&lt;code&gt;keyCode&lt;/code&gt;, et c),
    would reduce abstraction. These special cases are perfectly valid, but do not belong
    in the &lt;dfn&gt;Registry&lt;/dfn&gt;. Special case needs can either be hard-coded into end-implementation code 
    (using feature/capability detection) or, if the special-case logic is complex, programmed into an object that performs a task (such as an Adapter object). 
&lt;/p&gt;

&lt;p&gt;
    An example of an Adapter object would be 
    a &lt;a href=&quot;/learn/js/load/&quot;&gt;Content Load Adapter&lt;/a&gt; or a KeyEvent Adapter (key events are highly inconsistent across platforms).
    Such objects would be slightly higher-level and, having at least one dependency, would be less stable (though this is not a bad thing).

&lt;/p&gt;

&lt;h3&gt;Department Store JavaScript&lt;/h3&gt;
&lt;p&gt;
    [insert_popular_library_name_here] usually include more code than any one application could possibly use in an attempt to 
    cover the needs of every application. 
&lt;/p&gt;

&lt;p&gt;
    Libraries that add more functionality into one module than is usually needed, or create modules 
    that are not cohesive do so in spite of commonly known software package design concepts. The one-stop library approach is appealing because it allows developers to &quot;stop cobbling bits of javascript.&quot; 
&lt;/p&gt;

&lt;h3&gt;Performance (Again)&lt;/h3&gt;
&lt;p&gt;

    Load Time Performance problems can be acheived by creating custom javascript builds on the server. Hand-rolled &quot;combination&quot; files or utils files are fine for web sites with fewer pages. Sites that don&#39;t require 200k+ of additional javascript should not include such functionality.
&lt;/p&gt;
</description>
	    <dc:date>2008-01-04T21:42:32-06:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/uncategorized/2008/01/04/Event-Notification-System</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2008/01/04/Event-Notification-System?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2007/11/24/Browser-Detection">
	    <title>Browser Detection</title>
	    <link>http://dhtmlkitchen.com/news/default/2007/11/24/Browser-Detection</link>
        <description>&lt;p&gt;
There have been &lt;a href=&quot;http://www.jibbering.com/faq/faq_notes/not_browser_detect.html&quot;&gt;many&lt;/a&gt;, 
&lt;a href=&quot;http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_Pages#Developing_Cross_Browser.2FCross_Platform_Pages&quot;&gt;many&lt;/a&gt;, 
&lt;a href=&quot;http://www.quirksmode.org/js/support.html&quot;&gt;articles&lt;/a&gt; and 
&lt;a href=&quot;http://ejohn.org/blog/building-a-javascript-library/#comment-232407&quot;&gt;discussions&lt;/a&gt; that 
&lt;a href=&quot;http://ajaxian.com/archives/the-dangers-of-browser-detect&quot;&gt;eschew&lt;/a&gt;  
&lt;a href=&quot;http://dylanschiemann.com/2006/12/16/browser-detection-101-back-to-the-basics/&quot;&gt;browser&lt;/a&gt; &lt;a href=&quot;http://bclary.com/blog/2006/05/16/yahoo-and-the-evils-of-vendorversion-browser-detection/&quot;&gt;detection&lt;/a&gt;, yet it continues.
&lt;/p&gt;

&lt;p&gt;
	Despite all of this information, browser detection can be seen in most of the popular 
	JavaScript libraries including Prototype,
	YUI, Dojo, jQuery, and is present in applications such as the newly refactored GMail:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
	For a better Gmail experience, use a fully supported browser.   
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
	Browser detection is bad for many reasons:
&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Browsers change. &lt;/li&gt;
	&lt;li&gt;The User Agent string does not represent the browser reliably. &lt;/li&gt;
	&lt;li&gt;Even if it did, the browser doesn&#39;t represent feature support (See #1). &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Browser detection is unrelated to the problem it is trying
to solve.
&lt;/p&gt;

&lt;p&gt;
Browser detection makes code hard to maintain. It accomplishes this by 
requiring that the next version of [insert_browser_name] will also have to be tested 
and special-cased in the code.
&lt;/p&gt;

&lt;h3&gt;Alternative: Feature Detection&lt;/h3&gt;
&lt;p&gt;
For example, does the browser support &lt;code&gt;opacity&lt;/code&gt;? This can easily be determined:
&lt;/p&gt;

&lt;pre&gt;
if(&quot;opacity&quot;in el.style) { }
&lt;/pre&gt;

&lt;p&gt;
Support of &lt;code&gt;opacity&lt;/code&gt; has nothing to do with whether that
browser actually is an IE version, nor is the reverse true: IE does not imply 
support (or lack of support) for &lt;code&gt;opacity&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Detection for feature support does not suffer from maintenance 
problems when Internet Explorer decides to support &lt;code&gt;opacity&lt;/code&gt;. &lt;a href=&quot;http://dev.opera.com/articles/view/using-capability-detection/&quot;&gt;Capability detection&lt;/a&gt; takes feature detection one step further.
&lt;/p&gt;


&lt;p&gt;
Once the code has been properly designed and tested, it should not be a problem to maintain.
&lt;/p&gt;

&lt;p&gt;
I have learned this the hard way and have tried to remove browser detection from 
my drag code, though evidence of my mistake is still present. I had to refactor my drag code in specific cases where it checked for browsers identifying with an Opera User Agent string (removed checks to &lt;code&gt;ua.opera&lt;/code&gt;). My code still contains one conditional branch that needs to be refactored, As is, the script works in all of the modern browsers (this is subject to change).
&lt;/p&gt;

&lt;p&gt;
With browser detection, the &lt;a href=&quot;http://books.google.com/books?id=u13hVoYVZa8C&amp;amp;pg=PA29&amp;amp;lpg=PA29&amp;amp;dq=internal+quality+beck&amp;amp;source=web&amp;amp;ots=GI592TgTag&amp;amp;sig=NwxZNg0dSzxPkgZpn5GYtTcIe54&quot;
&gt;internal quality&lt;/a&gt; of the code suffers, even if the code works. This is because it introduces a dynamic aspect that must be maintained as the browsers change. &lt;a href=&quot;http://c2.com/cgi/wiki?ImplementationDoesTooMatter&quot;&gt;Implementation Matters&lt;/a&gt;.
&lt;/p&gt;

&lt;h3&gt;ToBoolean&lt;/h3&gt;

&lt;p&gt;
Be careful when testing for values of properties. Some values may evaluate to 
false in a boolean context.
&lt;/p&gt;

&lt;pre&gt;
&lt;strong&gt;// Error-prone, scrollTop may be 0, which would evaluate to false&lt;/strong&gt;
if (document.body.scrollTop) {
    // statements that work with scrollTop property
}
&lt;/pre&gt;

&lt;h3&gt;The new GMail&lt;/h3&gt;
&lt;p&gt;New code should definitely not rely on browser detection&lt;/p&gt; 
&lt;p&gt;
GMail, which was recently redesigned, still uses browser detection and also punishes users with the performance hit of a misused HTTP redirect (HTTP/1.x 302 Moved Temporarily), or, if GMail finds your browser&#39;s User Agent header unsuitable, it &lt;em&gt;two&lt;/em&gt; HTTP redirects. 
&lt;/p&gt;

&lt;p&gt;
In fact, when developing for mobile phones, I have found 
Chris Penderick&#39;s 
&lt;a href=&quot;https://addons.mozilla.org/en-US/firefox/addon/59&quot;&gt;UserAgent switcher&lt;/a&gt; useful. Unfortunately, this confuses GMail, messing up the 
rendering and even encoding of messages.
&lt;/p&gt;

&lt;p&gt;
GMail seems to be predominantly developed with a windows-centric mentality. 
This is evidenced by the lack of support for Command + S to save in 
Mac, and the Safari and Opera bugs witnessed in earlier versions of GMail.
&lt;/p&gt;

&lt;h3&gt;Google Groups&lt;/h3&gt;
&lt;p&gt;
	Google Groups also relies on faulty browser detection to block 
	&lt;a href=&quot;http://groups.google.com/groups/profile?myprofile&quot;&gt;certain features&lt;/a&gt;.
&lt;/p&gt;
&lt;p style=&quot;text-align:center&quot;&gt;
&lt;img src=&quot;/img/blog/browser-detection.jpg&quot; style=&quot;width: 620px;height:448px;margin-left:-10px;&quot;
alt=&quot;Google Groups Browser Error, in a Draggable, Floating DHTML Pane&quot; /&gt;
&lt;small&gt;Google Groups Browser Error, in a Draggable, Floating DHTML Pane&lt;/small&gt;
&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;
This feature is not supported in your browser. Download a copy of Firefox or Internet Explorer to upload your picture.
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
I find it ironic that my browser can be assumed to support the draggable, floating pane, and not suitable for uploading a picture. The floating pane is draggable from anywhere inside it, so it is impossible for a user who gets the error to select the error message text.
&lt;/p&gt;

&lt;h3&gt;Acknowledgements&lt;/h3&gt;
&lt;p&gt;
Google deserves proper recognition for providing a clear example of why browser detection is a bad practice.
&lt;/p&gt;

&lt;h3&gt;Contrasting Example&lt;/h3&gt;
&lt;p&gt;
For proof that an effective Ajax application can be developed &lt;em&gt;without&lt;/em&gt; browser detection, have a look at &lt;a 
href=&quot;http://www.google.com/reader/&quot;&gt;Google Reader&lt;/a&gt;. 
&lt;/p&gt;

&lt;p&gt;
Browser Detection is mostly a bad idea. 
&lt;/p&gt;

&lt;p&gt;
In the meantime, I&#39;m looking for a decent mail application that runs in the browser. Both Yahoo mail 
and GMail fall short of my expectations.
&lt;/p&gt;


</description>
	    <dc:date>2007-11-24T21:40:19-06:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/Browsers/2007/11/24/Browser-Detection</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2007/11/24/Browser-Detection?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2007/11/08/Opera-Bug-getComputedStyle-Returns-Margin-for-Unset-Top-Left-Values">
	    <title>Opera Bug: getComputedStyle Returns Margin for Unset Top/Left Values</title>
	    <link>http://dhtmlkitchen.com/news/default/2007/11/08/Opera-Bug-getComputedStyle-Returns-Margin-for-Unset-Top-Left-Values</link>
        <description>&lt;p&gt;
In Opera 9.2 &lt;code&gt;getComputedStyle(el, &#39;&#39;).getPropertyValue&lt;/code&gt; returns the margin value for &lt;code&gt;top&lt;/code&gt;/&lt;code&gt;left&lt;/code&gt; values when the &lt;code&gt;top&lt;/code&gt;/&lt;code&gt;left&lt;/code&gt; values aren&#39;t set. In Safari the returned values are &lt;samp&gt;&#39;auto&#39;&lt;/samp&gt; in this case.

&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;
&lt;p&gt;
&lt;a href=&quot;/test/bug/getComputedStyle.html&quot; onclick=&quot;window.open(this.href);return false;&quot;&gt;testcase showing bug in Opera and Safari&lt;/a&gt;
&lt;/p&gt;
&lt;h3&gt;Workaround&lt;/h3&gt;
&lt;p&gt;
    The way to avoid the problem is to explicitly add &lt;code&gt;top&lt;/code&gt; and &lt;code&gt;left&lt;/code&gt; values to the stylesheet:
&lt;/p&gt;

&lt;pre&gt;
#Test {
  top: 0;
  left: 0;
}&lt;/pre&gt;

&lt;p&gt;
Then &lt;code&gt;getComputedStyle&lt;/code&gt; will return correct values for &lt;code&gt;top&lt;/code&gt;/&lt;code&gt;left&lt;/code&gt; (&lt;samp&gt;0px&lt;/samp&gt;) in Opera 9 and Safari. 
&lt;/p&gt;

&lt;h3&gt;pixelXXX&lt;/h3&gt;
&lt;p&gt;
 A convenient alternative would be a &lt;code&gt;currentStyle.pixelLeft&lt;/code&gt;. Only Opera and IE support &lt;code&gt;currentStyle&lt;/code&gt; and only Opera supports &lt;code&gt;currentStyle.pixelLeft&lt;/code&gt;. (IE supports &lt;code&gt;style.pixelLeft&lt;/code&gt;; this only reads from the &lt;code&gt;style&lt;/code&gt; attribute)
&lt;/p&gt;

&lt;p&gt;
 Mozilla does not support &lt;code&gt;pixelXXX&lt;/code&gt; properties at all, though Opera, IE, and Safari 3 all do.
&lt;/p&gt;
</description>
	    <dc:date>2007-11-08T18:25:06-06:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/Browsers/2007/11/08/Opera-Bug-getComputedStyle-Returns-Margin-for-Unset-Top-Left-Values</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2007/11/08/Opera-Bug-getComputedStyle-Returns-Margin-for-Unset-Top-Left-Values?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2007/10/21/Iteration-Enumeration-Primitives-and-Objects">
	    <title>Iteration, Enumeration, Primitives, and Objects</title>
	    <link>http://dhtmlkitchen.com/news/default/2007/10/21/Iteration-Enumeration-Primitives-and-Objects</link>
        <description>&lt;script type=&quot;text/javascript&quot;&gt;
function assertEquals( baseId ) {
	var src = document.getElementById( baseId + &quot;-source&quot; );
	var actual = String(eval( src.textContent || src.innerText ));
	var expectedEl = document.getElementById( baseId + &quot;-expected&quot; );
	var expected = expectedEl.textContent || expectedEl.innerText;
	var resultEl = document.getElementById( baseId + &quot;-result&quot; );
	resultEl.innerHTML = &quot;&amp;nbsp;&quot;
	resultEl.firstChild.data = actual;
// Because IE does not properly preserve white space, Our test must check the text 
// w/ nbsp removed.
	var actualNoNbsp = actual.replace(/(?:\u00a0)|[\s\s]+/g,&quot;&quot;);
	var expectedNoNbsp = expected.replace(/(?:\u00a0)|[\s\s]+/g,&quot;&quot;);
    var actualTrimmed = String.trim( actualNoNbsp );
    var expectedTrimmed = String.trim( expectedNoNbsp );
	resultEl.className = (expectedNoNbsp == actualTrimmed ? &quot;correct&quot; : &quot;incorrect&quot;);
}

String.nl = &quot;clipboardData&quot; in window ? &quot;\r\n&quot; : &quot;\n&quot;;
String.trim = function( s ) {
    return s.replace(/^\s+|\s+$/g,&quot;&quot;);
}
&lt;/script&gt;

&lt;h1&gt;
    Iteration VS Enumeration
&lt;/h1&gt;
&lt;p&gt;
    When trying to understand a language, it is necessary to first understand the words.
&lt;/p&gt;

&lt;dl&gt;
    &lt;dt&gt;iteration&lt;/dt&gt;
        &lt;dd&gt;the &lt;code&gt;for&lt;/code&gt; statement (&lt;a 
 href=&quot;http://bclary.com/2004/11/07/#a-12.6.3&quot; 
	title=&quot;The for Statement&quot;&gt;&amp;#167;12.6.3&lt;/a&gt;)&lt;/dd&gt;
    &lt;dt&gt;enumeration&lt;/dt&gt;
        &lt;dd&gt;the &lt;code&gt;for in&lt;/code&gt; statement (&lt;a 
 href=&quot;http://bclary.com/2004/11/07/#a-12.6.4&quot; 
	title=&quot;The for in Statement&quot;&gt;&amp;#167;12.6.4&lt;/a&gt;)&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;
    While writing &lt;cite&gt;&lt;a href=&quot;/learn/js/enumeration/&quot;&gt;Enumeration and Object Oriented JavaScript&lt;/a&gt;&lt;/cite&gt;, I read and reasearched (I also grew a beard and lost about 20lbs in the process). I read blogs and libraries to see what other programmers were doing.  I came across a peculiar quote by Alex Russel (dojo fame).
&lt;/p&gt;

&lt;blockquote cite=&quot;http://alex.dojotoolkit.org/?p=620&quot;&gt;
&lt;p&gt;
&lt;q&gt;The contortions that Ajax toolkit vendors go through to 
keep iteration over JavaScript objects and primitives coherent is, 
quite simply, insane. Much of Dojo, in particular, is designed around this problem.&lt;/q&gt;
&lt;/p&gt;
&amp;mdash;Alex Russel
&lt;/blockquote&gt;

&lt;h3&gt;Iterate Over a Primitive?&lt;/h3&gt;

&lt;p&gt;
    That sounds &lt;em&gt;completely&lt;/em&gt; insane! I read through dojo.js (uncompressed), which was pretty heavily commented, but could find no such contortion.
&lt;/p&gt;

&lt;p&gt;
    I thought about this some more, and I&#39;m pretty sure it&#39;s not possible.
&lt;/p&gt;

&lt;p&gt;
	Attempting to &lt;em&gt;enumerate&lt;/em&gt; over a primitive value results in an Object 
	being created. That object (including its prototype chain) is then enumerated over.
&lt;/p&gt;

&lt;pre&gt;
Boolean.prototype.crap = &quot;useless&quot;;
for(var prop in true) { alert(true[prop]); }
if(!delete Boolean.prototype.crap) 
    alert(&quot;uh oh :(&quot;);
&lt;/pre
&gt;&lt;button onclick=&quot;eval(this.previousSibling.innerHTML);&quot;&gt;eval&lt;/button&gt;


&lt;p&gt;Attempting to enumerate over a primitive will result 
in the evaluation of the primitive in creation of an object
(&lt;a 
 href=&quot;http://bclary.com/2004/11/07/#a-9.9&quot; 
	title=&quot;The for in Statement&quot;&gt;&amp;#167;9.9&lt;/a&gt;), just as is with the property access operators, 
e.g. &lt;code&gt;true[ prop ]&lt;/code&gt; or &lt;code&gt;true.valueOf() === true&lt;/code&gt; or &lt;code&gt;1.1255.toPrecision(4);&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;
    The internal &lt;code&gt;ToObject&lt;/code&gt; method, in the above case, will return a &lt;code&gt;Boolean&lt;/code&gt; object holding the value &lt;code&gt;true&lt;/code&gt;. The property named &lt;var&gt;crap&lt;/var&gt; is resolved in &lt;code&gt;Boolean.prototype&lt;/code&gt;, holding the string value &lt;code&gt;&quot;useless&quot;&lt;/code&gt;.
&lt;/p&gt;

&lt;h3&gt;How About a String?&lt;/h3&gt;
&lt;p&gt;
    A string value might &lt;em&gt;seem&lt;/em&gt; to be iterated over (with some practical usefulness). In that case, a String &lt;em&gt;object&lt;/em&gt; would be created, holding the original string 
    value; you wouldn&#39;t actually be iterating over a string value; it would just &lt;em&gt;seem&lt;/em&gt; like it. 
&lt;/p&gt;

&lt;p&gt;
    The difference between a &lt;code&gt;String&lt;/code&gt; object and a string value, is explained in my entry &lt;a href=&quot;http://dhtmlkitchen.com/?category=/JavaScript/&amp;amp;date=2007/10/05/&amp;amp;entry=How-Property-Access-Works&quot;&gt;How Property Access Works&lt;/a&gt;.
&lt;/p&gt;


&lt;h3&gt;Practical Example of Iteration&lt;/h3&gt;
&lt;p&gt;Objects which support some form of sequential indexing of properties can be practically iterated over (&lt;a 
 href=&quot;http://bclary.com/2004/11/07/#a-12.6.3&quot; 
	title=&quot;The for Statement&quot;&gt;&amp;#167;12.6.3&lt;/a&gt;).
&lt;/p&gt;

&lt;p&gt;
    JavaScript provides two types of built-in objects that fit that description. 
&lt;/p&gt;

&lt;dl&gt;
    &lt;dt&gt;String&lt;/dt&gt;
    &lt;dd&gt;Property values are characters. indexed by a numerical String starting at &lt;code&gt;&quot;0&quot;&lt;/code&gt;&lt;/dd&gt;
    &lt;dt&gt;Array&lt;/dt&gt;
    &lt;dd&gt;Property values are anything. Indexed by a numerical String starting at &lt;code&gt;&quot;0&quot;&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;

&lt;h3&gt;Iterate over an Array&lt;/h3&gt;

&lt;p&gt;
    An &lt;code&gt;Array&lt;/code&gt; is an object 
	that is specialized by its &lt;code&gt;length&lt;/code&gt; property, it&#39;s &lt;code&gt;[[put]]&lt;/code&gt; method, 
	and its literal initializer syntax. &lt;code&gt;Array.prototype&lt;/code&gt; is sometimes modified 
    to add features that are not supported in certain browsers (like &lt;a href=&quot;http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6#Array_extras&quot;&gt;Array Extras&lt;/a&gt;, supported 
    in Webkit and Gecko, but not Opera 9.2  or IE 7).
&lt;/p&gt;

&lt;p&gt;
	Because &lt;code&gt;Array.prototype&lt;/code&gt; is often modified, &lt;a
	href=&quot;http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/&quot;
	&gt;it is not safe to enumerate over an &lt;code&gt;Array&lt;/code&gt;&lt;/a&gt; (unless you have a &lt;a 
	href=&quot;http://hexmen.com/blog/2006/12/iterating-over-sparse-arrays/&quot;&gt;sparse array&lt;/a&gt; and know how &lt;code&gt;hasOwnProperty&lt;/code&gt; works).
&lt;/p&gt;

&lt;p&gt;
    Non existent properties that need to be added by programmer-defined code do not exist, and therefore, cannot have the &lt;code&gt;DontEnum&lt;/code&gt; attribute. 
    For example, &lt;code&gt;Array.prototype.some&lt;/code&gt; does not exist on &lt;code&gt;Array.prototype&lt;/code&gt; in IE or Opera, but is non-enumerable, 
    native code in other browsers. 
&lt;/p&gt;
&lt;p&gt;To avoid enumerating over a property in the prototype chain, a cautious programmer will avoid using &lt;code&gt;for in&lt;/code&gt; on an Array, and use &lt;code&gt;hasOwnProperty&lt;/code&gt; when he does use &lt;code&gt;for in&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;    Library authors might consider adding only the top-level Extras, e.g. &lt;code&gt;Array.some&lt;/code&gt; to browsers that don&#39;t have &lt;code&gt;Array.some&lt;/code&gt;, and leaving the prototype alone. 
&lt;/p&gt;

&lt;h3&gt;Iterating Over a String&lt;/h3&gt;

&lt;p&gt;
    When the string value 
    is converted to a String Object, the characters in the value are accessible through the object. 
&lt;/p&gt;

&lt;p&gt;
    &lt;code&gt;String.prototype&lt;/code&gt; has a method to access character properties: &lt;code&gt;charAt&lt;/code&gt; (&lt;a 
 href=&quot;http://bclary.com/2004/11/07/#a-15.5.4.4&quot; 
	title=&quot;String.prototype.charAt&quot;&gt;&amp;#167;15.5.4.4&lt;/a&gt;). 
&lt;/p&gt;

&lt;p&gt;
    Here&#39;s where it gets interesting.
&lt;/p&gt;

&lt;p&gt;
    Gecko and Webkit implement &lt;em&gt;another&lt;/em&gt; way access to String index 
    properties: The property access operators &lt;code&gt;[ ]&lt;/code&gt;. This is not part of the official specification. 
&lt;/p&gt;
&lt;p&gt;  
    String&#39;s &lt;code&gt;[[Put]]&lt;/code&gt; method &lt;a href=&quot;http://bclary.com/2004/11/07/#a-8.6.2.2&quot; title=&quot;8.6.2.2 [[Put]](P, V)&quot;&gt;&amp;#167;8.6.2.2&lt;/a&gt; is specialized in Webkit. This seems to create some problems, as can be shown in an example.
&lt;/p&gt;
&lt;h3&gt;Example Using &lt;code&gt;[]&lt;/code&gt; on a String object&lt;/h3&gt;
&lt;pre id=&#39;magic-string-source&#39;&gt;(function(){
    String.prototype[&#39;2&#39;] = &quot;prot&quot;;
    String.prototype[&#39;4&#39;] = &quot;prot4&quot;;
    a = new String(&quot;012&quot;);
    a[1] = &quot;one&quot;;
    a[11] = &quot;eleven&quot;
    var result = [];
    for(var i = 0; i &amp;lt; a.length; i++)
        result.push(a.charAt(i) + &quot;, a[&#39;&quot;+i+&quot;&#39;] = &quot;+ a[i]);

    result.push(a.charAt(4)  // Should be &quot;&quot;.
        + &quot; , a[&#39;&quot;+ 4 +&quot;&#39;] = &quot;+ a[4] );
    
    result.push(a.charAt(11)  // Should be &quot;&quot;.
        + &quot; , a[&#39;&quot;+ 11 +&quot;&#39;] = &quot;+ a[11] );

    return result.join(String.nl);
})();
&lt;/pre&gt;
&lt;button onclick=&quot;assertEquals(&#39;magic-string&#39;);&quot;&gt;eval&lt;/button&gt;

&lt;table class=&quot;result&quot; style=&quot;width: 42em;&quot;&gt;
	&lt;tbody class=&quot;pre&quot;&gt;
        &lt;tr&gt;
            &lt;th&gt;Result&lt;/th&gt;
            &lt;th&gt;Expected&lt;/th&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td id=&quot;magic-string-result&quot; class=&quot;noresult&quot;&gt;                                            &lt;/td&gt;
            &lt;td id=&quot;magic-string-expected&quot;&gt;0, a[&#39;0&#39;] = 0
1, a[&#39;1&#39;] = one
2, a[&#39;2&#39;] = 2
, a[&#39;4&#39;] = prot4
, a[&#39;11&#39;] = eleven&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;table border=&quot;1&quot; class=&quot;result&quot; style=&quot;font-size: smaller&quot;&gt;
	&lt;thead&gt;
		&lt;tr&gt;
			&lt;th scope=&quot;row&quot; style=&quot;display:none&quot;&gt;Browser:&lt;/th&gt;
			&lt;th&gt;Internet Explorer&lt;/th&gt;
			&lt;th&gt;Mozilla&lt;/th&gt;
			&lt;th&gt;Opera 9.2&lt;/th&gt;
			&lt;th&gt;Safari 3&lt;/th&gt;
		&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
		&lt;tr&gt;
			&lt;th scope=&quot;row&quot; style=&quot;display:none&quot;&gt;Result:&lt;/th&gt;
            &lt;td class=&quot;incorrect&quot;&gt;0, a[&#39;0&#39;] = undefined
1, a[&#39;1&#39;] = one
2, a[&#39;2&#39;] = prot
, a[&#39;4&#39;] = prot4
, a[&#39;11&#39;] = eleven&lt;/td&gt;
			&lt;td&gt;0, a[&#39;0&#39;] = 0 
1, a[&#39;1&#39;] = one 
2, a[&#39;2&#39;] = 2 
, a[&#39;4&#39;] = prot4 
, a[&#39;11&#39;] = eleven&lt;/td&gt;
			&lt;td&gt;0, a[&#39;0&#39;] = 0
1, a[&#39;1&#39;] = one
2, a[&#39;2&#39;] = 2
 , a[&#39;4&#39;] = prot4
 , a[&#39;11&#39;] = eleven&lt;/td&gt;
			&lt;td class=&quot;incorrect unexpected&quot;&gt;0, a[&#39;0&#39;] = 0
1, a[&#39;1&#39;] = 1
2, a[&#39;2&#39;] = 2
, a[&#39;4&#39;] = prot4
, a[&#39;11&#39;] = eleven&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
    &lt;code&gt;String.prototype.charAt&lt;/code&gt; works consistently in all browsers. The property access operator results are more interesting.
&lt;/p&gt;

&lt;p&gt;
    In Gecko and Opera, property access (&lt;code&gt;[]&lt;/code&gt;, &lt;a href=&quot;http://bclary.com/2004/11/07/#a-11.2.1&quot;&gt;&amp;#167;11.2.1&lt;/a&gt;) on a &lt;code&gt;String&lt;/code&gt; checks the object for a property by the name given. If no property is found, if the string value of the property name is integral, &lt;code&gt;charAt&lt;/code&gt; (or its equivalent) is called, passing 
    the property, e.g &lt;code&gt;aString[1.0]&lt;/code&gt; is evaluated as &lt;code&gt;aString[&#39;1&#39;]&lt;/code&gt; (&lt;a 
 href=&quot;http://bclary.com/2004/11/07/#a-15.5.4.4&quot; 
	title=&quot;String.prototype.charAt&quot;&gt;&amp;#167;15.5.4.4&lt;/a&gt;). 
&lt;/p&gt;
&lt;p&gt;
    Webkit has the opposite result. Webkit seems to have broken &lt;code&gt;[[Put]]&lt;/code&gt; for &lt;code&gt;String&lt;/code&gt;.
&lt;/p&gt;

&lt;h4&gt;Observations on Property Access&lt;/h4&gt; 


&lt;h5&gt;Mozilla and Opera&lt;/h5&gt;
 &lt;ul&gt;
	&lt;li&gt;&lt;code&gt;a[&#39;0&#39;]&lt;/code&gt; returns the character at index &lt;code&gt;0&lt;/code&gt;, &quot;0&quot;. (goes through &lt;code&gt;charAt&lt;/code&gt;) &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a[&#39;1&#39;]&lt;/code&gt; is resolved to a property, the value &quot;one&quot; is returned.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;a[&#39;2&#39;]&lt;/code&gt; does not find a property on the object itself and does not call &lt;code&gt;[[Get]]&lt;/code&gt;. &lt;code&gt;charAt&lt;/code&gt; is called.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;a[&#39;11&#39;]&lt;/code&gt; is resolved to a property, the value &quot;one&quot; is returned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
    Gecko&#39;s &lt;code&gt;[[Put]]&lt;/code&gt; results in a property being added to the object. The property will not be returned 
    in &lt;code&gt;charAt&lt;/code&gt;, but will be returned with &lt;code&gt;[ ]&lt;/code&gt; (&lt;a 
 href=&quot;http://bclary.com/2004/11/07/#a-11.2.1&quot; 
	title=&quot;11.2.1 Property Accessors&quot;&gt;&amp;#167;11.2.1&lt;/a&gt;). 
&lt;/p&gt;


&lt;h5&gt;Webkit&lt;/h5&gt;
&lt;p&gt;
    Webkit seems to have a special algorithm for &lt;code&gt;[[Put]]&lt;/code&gt; on String. A property &lt;var&gt;P&lt;/var&gt; seems to be added 
    to a &lt;code&gt;String&lt;/code&gt; only when &lt;code&gt;ToNumber(&lt;var&gt;P&lt;/var&gt;)&lt;/code&gt; is greater than the String&#39;s length. 
&lt;/p&gt;


&lt;p&gt;
    The approach Webkit takes is a bad one. 
    I cannot see any reason why the Webkit team did this. A specialized &lt;code&gt;[[Put]]&lt;/code&gt; might be the result of the design under the hood and was a preventative 
    measure to keep the string from being modified (guess).
&lt;/p&gt;

&lt;p&gt;
    Unlike an &lt;code&gt;Array&lt;/code&gt;, 
    a &lt;code&gt;String&lt;/code&gt;&#39;s &lt;code&gt;length&lt;/code&gt; property is &lt;code&gt;ReadOnly&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
So we&#39;ve covered enumerating over a primitive (useless), iterating over an String and an Array. What about iterating over an object?
&lt;/p&gt;
&lt;h3&gt;Iterate over an Object&lt;/h3&gt;
&lt;p&gt;
    Its not usually practical, but it is &lt;em&gt;possible&lt;/em&gt;
&lt;/p&gt;
&lt;pre&gt;
var nlist = {
  n : 1,
  nn: 2,
  nnnn: 3,
  nnnnnnnn: 3
};

if(! &quot;console&quot; in window) {
    document.title = &quot;log: &quot;;
    window.console = { log : function(s){ document.title += s; };
}
var result = &quot;&quot;;
for( var p = &quot;n&quot;; nlist.hasOwnProperty(p); p+=p ) {
    console.log(nlist[p]);
}
&lt;/pre&gt;

&lt;p&gt;
    Iterating over a &lt;code&gt;BitSet&lt;/code&gt; as a String could be useful. 
&lt;/p&gt;

&lt;p&gt;
    It would be possible (possibly even useful) to iterate over (or use Array or String extras with) a &lt;code&gt;BitSet&lt;/code&gt; object.
    Fortunately, it&#39;s not that hard to write a 
    &lt;code&gt;BitSet&lt;/code&gt; class. 
&lt;/p&gt;

&lt;h3&gt;Enumeration is a Problem&lt;/h3&gt;
&lt;p&gt;
    Nearly every JavaScript library attempts to address the language limitations and browser bugs. 
    Enumeration needlessly complicates &lt;code&gt;Object&lt;/code&gt;. The libraries painfully show this to be true.
&lt;/p&gt;

&lt;p&gt;
    ECMAScript could instead
    provide &lt;code&gt;Enum&lt;/code&gt;, &lt;code&gt;SortedSet&amp;lt;&lt;var&gt;T&lt;/var&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;Map&amp;lt;&lt;var&gt;T&lt;/var&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;ListIterator&amp;lt;&lt;var&gt;T&lt;/var&gt;&amp;gt;&lt;/code&gt;. These collections would provide safer, more powerful  
    alternatives to the current problem.
&lt;/p&gt;

&lt;p&gt;
  I&#39;ve provided more analysis on the problem of enumeration in my article, 
 &lt;q&gt;&lt;a href=&quot;/learn/js/enumeration/&quot;&gt;Enumeration and Object Oriented JavaScript&lt;/a&gt;&lt;/q&gt;
&lt;/p&gt;
</description>
	    <dc:date>2007-10-21T18:12:00-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/JavaScript/2007/10/21/Iteration-Enumeration-Primitives-and-Objects</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2007/10/21/Iteration-Enumeration-Primitives-and-Objects?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2007/10/18/Browser-Stats">
	    <title>Browser Stats</title>
	    <link>http://dhtmlkitchen.com/news/default/2007/10/18/Browser-Stats</link>
        <description>Browser Stats

&lt;p&gt;
	Firefox is #1. Trumps MSIE.
&lt;/p&gt;

&lt;table border=&quot;1&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;&lt;th width=&quot;32&quot;&gt;&amp;nbsp;&lt;/th&gt;&lt;th&gt;Browsers&lt;/th&gt;&lt;th&gt;Grabber&lt;/th&gt;&lt;th&gt;Hits&lt;/th&gt;&lt;th&gt;Percent&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td width=&quot;32&quot;&gt;&lt;img src=&quot;/res/img/browser/firefox.png&quot; alt=&quot;FF&quot; title=&quot;&quot;/&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Firefox&lt;/b&gt;&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;79857&lt;/td&gt;&lt;td&gt;34.6 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/msie.png&quot; alt=&quot;&quot; title=&quot;IE&quot;/&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;MS Internet Explorer&lt;/b&gt;&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;74977&lt;/td&gt;&lt;td&gt;32.5 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/unknown.png&quot; alt=&quot;&quot; title=&quot;?&quot;/&gt;&lt;/td&gt;&lt;td&gt;&lt;span style=&quot;color: rgb(102, 102, 136);&quot;&gt;Unknown&lt;/span&gt;&lt;/td&gt;&lt;td&gt;?&lt;/td&gt;&lt;td&gt;63328&lt;/td&gt;&lt;td&gt;27.4 %&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/safari.png&quot; alt=&quot;&quot; title=&quot;Saf&quot;/&gt;&lt;/td&gt;&lt;td&gt;Safari&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;4006&lt;/td&gt;&lt;td&gt;1.7 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/opera.png&quot; alt=&quot;&quot; title=&quot;Op&quot;/&gt;&lt;/td&gt;&lt;td&gt;Opera&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;3607&lt;/td&gt;&lt;td&gt;1.5 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/mozilla.png&quot; alt=&quot;&quot; title=&quot;Mz&quot;/&gt;&lt;/td&gt;&lt;td&gt;Mozilla&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;3108&lt;/td&gt;&lt;td&gt;1.3 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/notavailable.png&quot; alt=&quot;&quot; title=&quot;XXX&quot;/&gt;&lt;/td&gt;&lt;td&gt;NetNewsWire&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;517&lt;/td&gt;&lt;td&gt;0.2 %&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/konqueror.png&quot; alt=&quot;&quot; title=&quot;Konq&quot;/&gt;&lt;/td&gt;&lt;td&gt;Konqueror&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;422&lt;/td&gt;&lt;td&gt;0.1 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/chimera.png&quot; alt=&quot;&quot; title=&quot;Chim&quot;/&gt;&lt;/td&gt;&lt;td&gt;Camino&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;252&lt;/td&gt;&lt;td&gt;0.1 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;img src=&quot;/res/img/browser/netscape.png&quot; alt=&quot;&quot; title=&quot;NS7&quot;/&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Netscape&lt;/b&gt;&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;91&lt;/td&gt;&lt;td&gt;0 %&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;span style=&quot;color: rgb(102, 102, 136);&quot;&gt;Others&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;123&lt;/td&gt;&lt;td&gt;0 %&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;
	Opera&#39;s settings allow surfers to easily spoof the &lt;samp&gt;User-Agent&lt;/samp&gt; header. Opera is probably &lt;em&gt;slightly&lt;/em&gt; underrepresented.
&lt;/p&gt;
</description>
	    <dc:date>2007-10-18T02:05:18-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/Browsers/2007/10/18/Browser-Stats</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2007/10/18/Browser-Stats?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2007/10/10/How-to-Tell-if-an-Object-is-a-Function">
	    <title>Type Checking in JavaScript</title>
	    <link>http://dhtmlkitchen.com/news/default/2007/10/10/How-to-Tell-if-an-Object-is-a-Function</link>
        <description>&lt;p&gt;
So how &lt;em&gt;do&lt;/em&gt; you tell if an object is a function?
&lt;/p&gt;

&lt;h3&gt;What&#39;s wrong with &lt;code&gt;typeof&lt;/code&gt;?&lt;/h3&gt;

&lt;pre&gt;
typeof o == &quot;function&quot;;
&lt;/pre&gt;

&lt;p&gt;
    Some browsers have unexpected behavior when using the &lt;code&gt;typeof&lt;/code&gt; operator with Host objects. 
&lt;/p&gt;
&lt;p&gt;
For example, in Safari, &lt;code&gt;typeof aNodeList == &quot;function&quot;&lt;/code&gt;
&lt;a href=&quot;http://bugs.webkit.org/show_bug.cgi?id=14547&quot;
title=&#39;typeof document.body.childNodes==&amp;quot;function&amp;quot; (&amp;quot;object&amp;quot; expected)&#39;&gt;bug 14547&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Mozilla  returns &lt;code&gt;&quot;function&quot;&lt;/code&gt; for &lt;code&gt;typeof&lt;/code&gt; on &lt;code&gt;RegExp&lt;/code&gt; objects (&lt;a href=&quot;https://bugzilla.mozilla.org/show_bug.cgi?id=61911&quot;
title=&#39;typeof(/regExp/) should return &amp;quot;object&amp;quot;, not &amp;quot;function&amp;quot;&#39;&gt;bug 14547&lt;/a&gt;), and &lt;code&gt;object&lt;/code&gt; elements (&lt;a href=&quot;https://bugzilla.mozilla.org/show_bug.cgi?id=268945&quot;
title=&#39;Applet tag returns function instead of object with typeof JavaScript method&#39;&gt;bug 268945&lt;/a&gt;,
&lt;a href=&quot;
https://bugzilla.mozilla.org/show_bug.cgi?id=296858&quot;
title=&quot;DOM API: typeof HTMLObjectElement is &#39;function&#39;&quot;&gt;bug 296858&lt;/a&gt;).

&lt;/p&gt;

&lt;h3&gt;The instanceof Operator&lt;/h3&gt;
&lt;p&gt;
    The more flexible alternative to &lt;code&gt;typeof&lt;/code&gt; is &lt;code&gt;instanceof&lt;/code&gt;. 
&lt;/p&gt;

&lt;pre&gt;
    return it instanceof Function;
&lt;/pre&gt;

&lt;p&gt;
Unfortunately, &lt;code&gt;instanceof&lt;/code&gt; will not return the desired result across frames. Each window has a different global object, so &lt;code&gt;Function&lt;/code&gt; in frame 1 is not &lt;code&gt;Function&lt;/code&gt; in frame 2.
&lt;/p&gt;

&lt;p&gt;
  When working across frames, the &lt;code&gt;constructor&lt;/code&gt; property and &lt;code&gt;isPrototypeOf&lt;/code&gt; will have the same problems as &lt;code&gt;instanceof&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
For example, an &lt;code&gt;iframe&lt;/code&gt; with a function &lt;code&gt;f&lt;/code&gt;, and the following code in the parentWindow:
&lt;/p&gt;

&lt;pre&gt;
var i = document.getElementsByTagName(&quot;iframe&quot;)[0];
var iframeWindow = i.contentWindow;
iframeWindow.f instanceof Function; // false
typeof iframeWindow.f == &quot;function&quot;; // true;
&lt;/pre&gt;

&lt;p&gt;
    It should be easy to know what an object&#39;s type is. Why does JavaScript 
make this so hard?
&lt;/p&gt;

&lt;h3&gt;What Do the Libraries Do?&lt;/h3&gt;
&lt;p&gt;
    Dojo had the idea of depending on a combination of the &lt;code&gt;userAgent&lt;/code&gt; string, the return 
    value of the object&#39;s &lt;code&gt;toString&lt;/code&gt; method, and implicit type conversion that happens with the Equals Operator &lt;a href=&quot;http://bclary.com/2004/11/07/#a-11.9.3&quot;&gt;&amp;#167;11.9.3&lt;/a&gt;.
&lt;/p&gt;

&lt;pre&gt;
if(dojo.isBrowser &amp;amp;&amp;amp; dojo.isSafari){
 // only slow this down w/ gratuitious casting in Safari since it&#39;s what&#39;s b0rken
  dojo.isFunction = function(/*anything*/ it){
    if((typeof(it) == &quot;function&quot;) 
      &amp;amp;&amp;amp; (it == &quot;[object NodeList]&quot;)){ return false; }
  return (typeof it == &quot;function&quot; || it instanceof Function); // Boolean
  }
}else{
  dojo.isFunction = function(/*anything*/ it){
    return (typeof it == &quot;function&quot; || it instanceof Function); // Boolean
  }
}&lt;/pre&gt;

&lt;p&gt;
    This is to address Safari&#39;s unreliable results when using the &lt;code&gt;typeof&lt;/code&gt; operator on one particular host object. 
&lt;/p&gt;
&lt;p&gt;
The function will fail in Safari 3 when passed an &lt;code&gt;HTMLCollection&lt;/code&gt; (try &lt;code&gt;typeof document.links&lt;/code&gt;). It will fail for any other object that Safari thinks implements call. 
&lt;/p&gt;

&lt;p&gt;
  The problem is not that dojo forgot to check for &lt;code&gt;HTMLCollection&lt;/code&gt;. Web developers should not need to know such obscurities.
&lt;/p&gt;

&lt;h3&gt;jQuery : isFunction&lt;/h3&gt;
&lt;p&gt;
    The jQuery code stumbles on this problem:
&lt;/p&gt;

&lt;pre&gt;
// This may seem like some crazy code,
// but trust me when I say that this
// is the only cross-browser way to do this. --John

  isFunction: function( fn ) {
    return !!fn &amp;amp;&amp;amp; typeof fn != &quot;string&quot; &amp;amp;&amp;amp; !fn.nodeName &amp;amp;&amp;amp; 
      fn.constructor != Array &amp;amp;&amp;amp; /function/i.test( fn + &quot;&quot; );
  },
&lt;/pre&gt;

&lt;p&gt;
    Lets break down this crazy code to see what it&#39;s doing.
&lt;/p&gt;

&lt;pre&gt;
( !!fn ).......................convert to boolean value. null, et c are out
( typeof fn != &quot;string&quot; )......not a string value (String objects pass here) 
( !fn.nodeName )...............Not an object with a truthy nodeName property
( fn.constructor != Array )....Not an Array object constructed in this frame
( /function/i.test( fn + &quot;&quot; )..toString contains &quot;function&quot;
&lt;/pre&gt;

&lt;p&gt;
    Does it work?
&lt;/p&gt;
&lt;pre&gt;
// Case 1: testObj is not a function. 
var testObj = {
    toString: function() {
        return &quot;This object contains only one function.&quot;
    }
};

// Case 2: testString is not a function.
var testString = new String(&quot;hey! This is a String, not a function!&quot;);

// Case 3: WidgetFactory is a function.
function WidgetFactory( id ){ }
WidgetFactory.toString = function() { return &quot;WidgetFactory&quot;; };

// Case 4 : An Array in the frameWindow is not a function.
// var someArray = [ function(){}, &quot;Simplify functional testing.&quot; ];

// Here&#39;s the test:
var frameWindow = document.getElementsByTagName(&quot;iframe&quot;)[0].contentWindow;
isFunction( testObj ); // true.
isFunction( testString ); // true.
isFunction( WidgetFactory ); // false.
isFunction( frameWindow.someArray ); // true, contains &quot;function&quot; (twice).
&lt;/pre&gt;

&lt;p&gt;
    Doesn&#39;t work that well, does it? 
&lt;/p&gt;

&lt;p&gt;
    Running code doesn&#39;t lie. All four tests failed.
&lt;/p&gt;

&lt;p&gt;
    The jQuery function will give the correct result sometimes, but not in any of the cases above. This is because jQuery is relying on the result from the function&#39;s &lt;code&gt;toString&lt;/code&gt;. 
&lt;/p&gt;

&lt;p&gt;
 These cases are quite obvious and likely cases where the above method will fail. Nothing tricky about it.
&lt;/p&gt;

&lt;p&gt;
    The value returned by &lt;code&gt;toString&lt;/code&gt; should not generally be relied upon, and especially not with a Host object. 
&lt;/p&gt;

&lt;p&gt;
    JavaScript forces developers to rely on &lt;code&gt;toString&lt;/code&gt; with &lt;code&gt;Date&lt;/code&gt;
    or &lt;code&gt;Number&lt;/code&gt; objects. This is an API design of the language that goes against convention. Such design should not influence a JavaScript library author&#39;s design decisions.
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;q&gt;
The disadvantage of specifying the format of the &lt;code&gt;toString&lt;/code&gt; return value is that once you&#39;ve specified it, you&#39;re stuck with it for life.&lt;/q&gt;&amp;mdash;Joshua Bloch, quote from Effective Java
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
Method &lt;code&gt;toString&lt;/code&gt; should not, generally speaking, be relied upon.
Instead,
  &lt;code&gt;toString&lt;/code&gt; should be used for diagnostic messages.
&lt;/p&gt;
&lt;h4&gt;String Objects are Not string Values&lt;/h4&gt;
&lt;p&gt;
 Web developers sometimes get confused with string values and String objects. My tutorial on &lt;a href=&quot;http://dhtmlkitchen.com/?category=/JavaScript/&amp;amp;date=2007/10/05/&amp;amp;entry=How-Property-Access-Works&quot;
&gt;how property access operators work&lt;/a&gt;, clearly explains this fundamental concept. 
&lt;/p&gt;
&lt;p&gt;
    Here&#39;s another example from Dojo.
&lt;/p&gt;

&lt;pre&gt;dojo.isAlien = function(/*anything*/ it){
  // summary: 
  // Returns true if it is a built-in function or some other kind of
  // oddball that *should* report as a function but doesn&#39;t
  if(!it){ return false; }
  return !dojo.isFunction(it) 
    &amp;amp;&amp;amp; /\{\s*\[native code\]\s*\}/.test(String(it)); // Boolean
}
&lt;/pre&gt;

&lt;p&gt;
 Besides conjuring up images of little green men, function
 &lt;code&gt;isAlien&lt;/code&gt; returns true for anything that &lt;code&gt;isFunction&lt;/code&gt; returned false for and also contains &lt;code&gt;&quot;[native code]&quot;&lt;/code&gt; in the object&#39;s string value. The intent seems to be to check if a function might be a function even when &lt;code&gt;typeof&lt;/code&gt; failed.
&lt;/p&gt;

&lt;p&gt;
    I have a very hard time believing that any unmodified built-in function does not return &quot;function&quot; for &lt;code&gt;typeof&lt;/code&gt;. 
    The comment needs more detail.
    It would be interesting to see the dojo comment proven true. At least they put an explanatory comment in. 
&lt;/p&gt;

&lt;p&gt;
  The comment implies that a built-in function might not return &quot;function&quot; but does not provide a case where this is true. The code is based on that assumption and assumes that such an object&#39;s String value will contain &lt;samp&gt;&quot;[native code]&quot;&lt;/samp&gt;.
&lt;/p&gt;

&lt;p&gt;
  This approach suffers from the same problems that jQuery&#39;s &lt;code&gt;isFunction&lt;/code&gt; suffered from. If any object&#39;s string value contains &lt;code&gt;&quot;[native code]&quot;&lt;/code&gt;, &lt;code&gt;isAlien&lt;/code&gt; will return true. Granted, &lt;code&gt;&quot;[native code]&quot;&lt;/code&gt; is less common than &lt;code&gt;&quot;function&quot;&lt;/code&gt;, but buying vowels isn&#39;t the answer. 
&lt;/p&gt;

&lt;p&gt;
Function &lt;code&gt;isAlien&lt;/code&gt; provides a way for non-callable objects to slip through as functions. It&#39;s inclusive, like the jQuery function, and just as dangerous. It does this by relying on &lt;code&gt;[native code]&lt;/code&gt; making deductions about the Object based on its &lt;code&gt;toString&lt;/code&gt;&#39;s return value.
&lt;/p&gt;

&lt;p&gt;
    Return values from &lt;code&gt;toString&lt;/code&gt; should not, in general, be relied on
&lt;/p&gt;


&lt;blockquote&gt;
&lt;p&gt;
&lt;q&gt;If it walks like a duck and quacks like a duck, it must be a duck.&lt;/q&gt;
&amp;mdash;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;safari-anwser&quot;&gt;How to Determine if an Object is Really a Function&lt;/h3&gt;

&lt;p&gt;
    Functions are unique in a few ways. The return value for &lt;code&gt;toString&lt;/code&gt; is not one of them. 
&lt;/p&gt;
&lt;p&gt;
It seems safer to introspect the object based on a unique characteristic. 
&lt;/p&gt;


&lt;h3&gt;Update - Oct 13&lt;/h3&gt;

&lt;p&gt;
  A solution is needed. Here is one more offering, introspecting the &lt;code&gt;constructor&lt;/code&gt; property, accompanied with a &lt;a 
href=&quot;/jstest/isFunctionTest.html&quot;&gt;testcase&lt;/a&gt;. 
&lt;/p&gt;

&lt;pre style=&quot;border-color: #090;&quot;&gt;
function isFunction(fn) {
  if(typeof fn != &quot;function&quot;) return false;
&lt;span class=&quot;comment&quot;&gt;// Now check the constructor property. &lt;/span&gt;
  if(typeof fn.constructor != &quot;function&quot;) return false;
&lt;span class=&quot;comment&quot;&gt;// If constructor is Function, then constructor.prototype will have call.&lt;/span&gt;
  return fn.constructor.prototype.hasOwnProperty( &quot;call&quot; );
}
&lt;/pre&gt;

&lt;p&gt;
That&#39;s some ugly looking code, huh? Who has a better one? 
&lt;/p&gt;

&lt;p&gt;
Try to provide case where the &lt;a 
href=&quot;/jstest/isFunctionTest.html&quot;&gt;testcase&lt;/a&gt; will fail. 
&lt;/p&gt;

&lt;p&gt;
  We can make more functions for &lt;code&gt;isArray&lt;/code&gt;, et c. What a pain in the ass. This should be easy. JavaScript makes it hard.
&lt;/p&gt;

&lt;h3&gt;Type Checking - the Status Quo&lt;/h3&gt;
&lt;p&gt;
  JavaScript needs a way to perform equivalence check. This might be helpful for cross frame issues. 
&lt;/p&gt;

&lt;p&gt;
  JavaScript provides &lt;code&gt;instanceof&lt;/code&gt; and &lt;code&gt;typeof&lt;/code&gt;, but  neither are sufficient
&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;instanceof&lt;/code&gt; fails across frames. It doesn&#39;t work with primitives, e.g. &lt;code&gt;&quot;foo&quot; instanceof String&lt;/code&gt; is false.
&lt;/p&gt;

&lt;p&gt;
 The &lt;code&gt;typeof&lt;/code&gt; operator has a limited number of return types. typeof doesn&#39;t support all of the built-ins properly (e.g. &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Error&lt;/code&gt;, &lt;code&gt;Date&lt;/code&gt;, and &lt;code&gt;null&lt;/code&gt; are all &quot;object&quot;) and doesn&#39;t support any user-defined constructors. Typeof is allowed to return an implementation-dependent value for 
host objects. Safari conforms, but in an annoying way that causes problems.
&lt;/p&gt;

&lt;table summary=&quot;typeof Operator&quot; &gt;
        &lt;thead&gt;
          &lt;tr&gt;
            &lt;th&gt;Type&lt;/th&gt;

            &lt;th&gt;Result&lt;/th&gt;
          &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody style=&quot;font-family:Courier New, monospace;&quot;&gt;
          &lt;tr&gt;
            &lt;td&gt;Undefined&lt;/td&gt;
            &lt;td&gt;&quot;undefined&quot;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Null&lt;/td&gt;
            &lt;td&gt;&quot;object&quot;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Boolean&lt;/td&gt;
            &lt;td&gt;&quot;boolean&quot;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Number&lt;/td&gt;
            &lt;td&gt;&quot;number&quot;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;String&lt;/td&gt;
            &lt;td&gt;&quot;string&quot;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Object &lt;span style=&quot;font-family: sans-serif&quot;&gt;(native and doesn&#39;t implement &lt;a href=&quot;http://bclary.com/2004/11/07/#a-13.2.1&quot;&gt;[[Call]]&lt;/a&gt;)&lt;/span&gt;&lt;/td&gt;

            &lt;td&gt;&quot;object&quot;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Object &lt;span style=&quot;font-family: sans-serif&quot;&gt;(native and implements &lt;a href=&quot;http://bclary.com/2004/11/07/#a-13.2.1&quot;&gt;[[Call]]&lt;/a&gt;)&lt;/span&gt;&lt;/td&gt;
            &lt;td&gt;&quot;function&quot;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Object (host)&lt;/td&gt;
            &lt;td&gt;Implementation-dependent&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
&lt;p&gt;
Internet explorer includes an additional &lt;samp&gt;&quot;unknown&quot;&lt;/samp&gt;.
&lt;/p&gt;

&lt;p&gt;
      Typechecking is an area that JavaScript, as a language, needs to improve.
&lt;/p&gt;

&lt;h4&gt;
Update...
&lt;/h4&gt;

&lt;p&gt;
Brendan just posted on the ES4 List:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;q&gt;
The &lt;code&gt;is&lt;/code&gt; operator tests universal or Platonic type, which involves
shared, immutable type descriptors that do not vary across windows or
frames. So
&lt;/q&gt;&lt;/p&gt;
&lt;pre&gt;
(it is Callable)
&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;
  That looks very useful. Immutable objects are powerful building blocks for other object types. Looks like there might be the possibility of other callable types (or subclass of &lt;code&gt;Function&lt;/code&gt;) in ES4.
&lt;/p&gt;
</description>
	    <dc:date>2007-10-10T16:57:52-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/JavaScript/2007/10/10/How-to-Tell-if-an-Object-is-a-Function</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2007/10/10/How-to-Tell-if-an-Object-is-a-Function?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2007/10/05/How-Property-Access-Works">
	    <title>How Property Access Works</title>
	    <link>http://dhtmlkitchen.com/news/default/2007/10/05/How-Property-Access-Works</link>
        <description>&lt;p&gt;
    It&#39;s important for web developers to understand how the property 
    access operators work. 
    Here&#39;s a javascript basic lesson that every web developer should know:
&lt;/p&gt;
&lt;div style=&quot;display: none&quot;&gt;
&lt;h4&gt;Using an XML Reader?&lt;/h4&gt;
&lt;p&gt;The live JavaScript code won&#39;t run in your reader, but can be run 
&lt;a href=&quot;http://dhtmlkitchen.com/?category=/JavaScript/&amp;amp;date=2007/10/03/&amp;amp;entry=Opera-clobbers-object-prototype-with-FunctionExpression&quot;&gt;on the site&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;pre id=&quot;svalue-source&quot;&gt;(function(){
var a = &quot;A String?&quot;;

a.isString = true;

return a.isString;
})();&lt;/pre&gt;
&lt;p&gt;
    What should be returned? Will it be the value &lt;code&gt;true&lt;/code&gt;?
&lt;/p&gt;

&lt;button onclick=&quot;assertEquals(&#39;svalue&#39;);&quot;&gt;eval&lt;/button&gt;&lt;code id=&quot;svalue-result&quot;&gt;&lt;/code&gt;
&lt;code id=&quot;svalue-expected&quot; style=&quot;left: -20em; position: absolute;&quot;&gt;undefined&lt;/code&gt;

&lt;p&gt;
    What happened to the &lt;code&gt;isString&lt;/code&gt; property? 
&lt;/p&gt;

&lt;p&gt;
    To answer that question, you need to know how the property access operator works.
&lt;/p&gt;

&lt;p&gt;
    The property access operator converts the &lt;em&gt;value&lt;/em&gt; of its left hand operand (if it is a primitive) to an object and gets the property off that object. 
&lt;/p&gt;

&lt;p&gt;
    Effectively, the above example is equivalent to:
&lt;/p&gt;
&lt;pre&gt;
    var a = &quot;A String?&quot;;
    Object(a).isString = true;
    return Object(a).isString; // undefined
&lt;/pre&gt;

&lt;p&gt;
    An object is created, assigned the property &lt;code&gt;isString&lt;/code&gt; with the value &lt;code&gt;true&lt;/code&gt;, and then becomes 
    inaccessible by program code.
&lt;/p&gt;
 
&lt;p&gt;
    How about assigning properties to &lt;code&gt;String&lt;/code&gt; objects?
&lt;/p&gt;

&lt;pre id=&quot;sobject-source&quot;&gt;(function(){
var a = &quot;Axel&quot;;
a = new String(a);

a.foo = 123;

return a.foo;
})();&lt;/pre&gt;
&lt;button onclick=&quot;assertEquals(&#39;sobject&#39;);&quot;&gt;eval&lt;/button&gt;&lt;code id=&quot;sobject-result&quot;&gt;&lt;/code&gt;
&lt;code id=&quot;sobject-expected&quot; style=&quot;left: -20em; position: absolute;&quot;&gt;123&lt;/code&gt;

&lt;p&gt;
    Variable &lt;code&gt;a&lt;/code&gt; is now a String object. Objects can have properties.
&lt;/p&gt;

&lt;p&gt;
    The same conversion does not, unfortunately, take place with the &lt;code&gt;instanceof&lt;/code&gt; operator.
&lt;/p&gt;

&lt;pre id=&quot;instanceof-source&quot;&gt;(function(){
    var a = &quot;Al Gore&quot;;
    return a instanceof String;
})();&lt;/pre&gt;&lt;button onclick=&quot;assertEquals(&#39;instanceof&#39;);&quot;&gt;eval&lt;/button&gt;&lt;code id=&quot;instanceof-result&quot;&gt;&lt;/code&gt;
&lt;code id=&quot;instanceof-expected&quot; style=&quot;left: -20em; position: absolute;&quot;&gt;false&lt;/code&gt;

&lt;p&gt;
    It is still possible to check the &lt;code&gt;constructor&lt;/code&gt; property of the object that is created by the 
    property access operator.
&lt;/p&gt;

&lt;pre id=&quot;constructor-source&quot;&gt;(function(){
    var a = &quot;avatar&quot;;
    return a.constructor === String;
})();&lt;/pre&gt;&lt;button onclick=&quot;assertEquals(&#39;constructor&#39;);&quot;&gt;eval&lt;/button&gt;&lt;code id=&quot;constructor-result&quot;&gt;&lt;/code&gt;
&lt;code id=&quot;constructor-expected&quot; style=&quot;left: -20em; position: absolute;&quot;&gt;true&lt;/code&gt;

&lt;p&gt;
    The &lt;code&gt;typeof&lt;/code&gt; operator returns &quot;string&quot; for string values and &quot;object&quot; for &lt;code&gt;String&lt;/code&gt; objects.
&lt;/p&gt;

&lt;pre id=&quot;typeof-source&quot;&gt;(function(){
    var a = &quot;apple&quot;;
    var b = new String(&quot;bork&quot;);
    return typeof a + &quot;; &quot; + typeof b;
})();&lt;/pre&gt;&lt;button onclick=&quot;assertEquals(&#39;typeof&#39;);&quot;&gt;eval&lt;/button&gt;&lt;code id=&quot;typeof-result&quot;&gt;&lt;/code&gt;
&lt;code id=&quot;typeof-expected&quot; style=&quot;left: -20em; position: absolute;&quot;&gt;string; object&lt;/code&gt;

&lt;p&gt;
    Primitive values are not objects. Don&#39;t let the property access operators fool you.
&lt;/p&gt;

&lt;a title=&quot;[MDC] Core JavaScript 1.5 Reference:Global Objects:String&quot; href=&quot;http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String&quot;
&gt;Core JavaScript 1.5 Reference:Global Objects:String&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot; defer=&quot;defer&quot;&gt;
/**
 * compares text-content of expected to actual result by using eval.
 * if result is incorrect, adds the &quot;incorrect&quot; className.
 */
function assertEquals( baseId ) {
	var src = document.getElementById( baseId + &quot;-source&quot; );
	var actual = String(eval( src.textContent || src.innerText ));

	var expectedEl = document.getElementById( baseId + &quot;-expected&quot; );
	var expected = expectedEl.textContent || expectedEl.innerText;
	var resultEl = document.getElementById( baseId + &quot;-result&quot; );
	resultEl.innerHTML = &quot;&amp;nbsp;&quot;
	resultEl.firstChild.data = actual;
// Because IE does not properly preserve white space, Our test must check the text 
// w/ nbsp removed.
	var actualNoNbsp = actual.replace(/(?:\u00a0)|[\s\s]+/g,&quot;&quot;);
	var expectedNoNbsp = expected.replace(/(?:\u00a0)|[\s\s]+/g,&quot;&quot;);
    var actualTrimmed = String.trim( actualNoNbsp );
    var expectedTrimmed = String.trim( expectedNoNbsp );
	resultEl.className = (expectedNoNbsp == actualTrimmed ? &quot;correct&quot; : &quot;incorrect&quot;);
}
String.trim = function( s ) {
    return s.replace(/^\s+|\s+$/g,&quot;&quot;);
};
&lt;/script&gt;
</description>
	    <dc:date>2007-10-05T03:09:22-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/JavaScript/2007/10/05/How-Property-Access-Works</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2007/10/05/How-Property-Access-Works?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
            <item rdf:about="http://dhtmlkitchen.com/news/default/2007/10/03/Opera-clobbers-object-prototype-with-FunctionExpression">
	    <title>Opera Clobbers Object.prototype with FunctionExpression? </title>
	    <link>http://dhtmlkitchen.com/news/default/2007/10/03/Opera-clobbers-object-prototype-with-FunctionExpression</link>
        <description>&lt;script type=&quot;text/javascript&quot;&gt;
function evalTextContent(src) {
    var el = src;
    if(typeof src == &quot;string&quot;)
    	var el = document.getElementById(src);
	new Function(el.textContent || el.innerText)();
}
&lt;/script&gt;

&lt;pre&gt;(function(){

propertyIsEnumerable = function(){ alert(123); };
( {} ).propertyIsEnumerable( &quot;name&quot; );
})();&lt;/pre&gt;&lt;button onclick=&quot;evalTextContent(this.previousSibling)&quot;&gt;eval&lt;/button&gt;


&lt;p&gt;
In opera 9.2, both Mac and Windows, I get the alert. 
&lt;/p&gt;

&lt;p&gt;
This happens when adding a FunctionExpression property to the global object. The problem only happens when the property name corresponds to a native method.
&lt;/p&gt;

&lt;p&gt;
 Hopefully Futhark, Opera 9.5&#39;s new script engine that will replace &lt;a href=&quot;http://en.wikipedia.org/wiki/Linear_b_(script_engine)&quot;&gt;linear_b&lt;/a&gt;, will address this problem.
&lt;/p&gt;

</description>
	    <dc:date>2007-10-03T20:01:03-05:00</dc:date>
	                                <wfw:comment>http://dhtmlkitchen.com/commentapi/default/JavaScript/2007/10/03/Opera-clobbers-object-prototype-with-FunctionExpression</wfw:comment>
            <wfw:commentRss>http://dhtmlkitchen.com/news/default/2007/10/03/Opera-clobbers-object-prototype-with-FunctionExpression?page=comments&amp;flavor=rss2</wfw:commentRss>
            </item>
    
</rdf:RDF>
