Web Info & Tutorials

November 14th, 2007

JAVASCRIPT METHOD OVERLOADING

John Resig has posted on JavaScript Method Overloading where he discusses the ability to run different code depending on the number of arguments passed into the same function name. The type of arguments are not considered, which is where JavaScript 2 comes in.

The code he uses:

JAVASCRIPT:
  1.  
  2. // addMethod - By John Resig (MIT Licensed)
  3. function addMethod(object, name, fn){
  4.     var old = object[ name ];
  5.     object[ name ] = function(){
  6.     if ( fn.length == arguments.length )
  7.        return fn.apply( this, arguments );
  8.     else if ( typeof old == 'function' )
  9.        return old.apply( this, arguments );
  10.    };
  11. }
  12.  
  13. // Now setup the methods
  14.  
  15. function Users(){
  16.   addMethod(this, "find", function(){
  17.     // Find all users...
  18.   });
  19.   addMethod(this, "find", function(name){
  20.     // Find a user by name
  21.   });
  22.   addMethod(this, "find", function(first, last){
  23.     // Find a user by first and last name
  24.   });
  25. }
  26.  
  27. // Now use the methods
  28. var users = new Users();
  29. users.find(); // Finds all
  30. users.find("John"); // Finds users by name
  31. users.find("John", "Resig"); // Finds users by first and last name
  32. users.find("John", "E", "Resig"); // Does nothing
  33.  

John ends with the caveats:

  • The overloading only works for different numbers of arguments - it doesn't differentiate based on type, argument names, or anything else. (ECMAScript 4/JavaScript 2, however, will have this ability - called Multimethods - I'm quite excited.)
  • All methods will some function call overhead. Thus, you'll want to take that into consideration in high performance situations.

Now, the secret sauce is all going back to the fn.length expression. This isn't very well known, but all functions have a length property on them. This property equates to the number of arguments that the function is expecting. Thus, if you define a function that accepts a single argument, it'll have a length of 1, like so:

(function(foo){}).length == 1

November 14th, 2007

DISSECTING DIJIT: DOJO WIDGETS

One of the best parts of Dojo 1.0 has been how often the team has been blogging of all things. It has been great!

Mike Wilcox has posted about Dijit, the widget platform for Dojo 1.0.

His piece covers dojo.parser, dojo.declare, the widget markup, the lifecycle of widgets, and more. Great stuff.

The lifecycle is very thorough:

preamble()
Originating in dojo.declare, preamble is a new Dojo Core feature. It’s a pre-constructor accessory. By analogy, preamble is to constructor as postMixInProperties is to postCreate. Since preamble gets the same arguments as the constructor, you may extend another object, and jump in front of the constructor and change the arguments.
constructor()
Originating in dojo.declare, constructor has a new usage pattern. Previously, it fired last, which I didn’t find particularly useful nor accurate. It now fires early in the widget lifecycle, allowing early initialization with the arguments passed into the object. While more common in use, it’s not exactly necessary, as Dijit handles the job of converting your arguments into object properties.
postMixInProperties()
Originating in dijit._Widget, postMixInProperties is used more commonly by widget developers. That said, some of its duties are superseded by the addition of constructor and preamble. Its main purpose is firing after the properties have been set, but before the widget has been parsed and created. Pre-creation work on widget properties is typically done in this method.
postCreate()
Originating in dijit._Widget, postCreate is the “heavy lifter” of Dijit. This fires after creation, but before the widget is rendered to the page. At this time in the widget lifecycle, you have access to the widget’s nodes, so additional parsing, connections, styling, or even attaching more widgets is possible.
startup()
Originating in dijit._Widget, startup is somewhat misunderstood. startup doesn’t fire unless the widget is a child of another widget. And then it only fires after it, and all of its siblings have been created. Then they all fire together.
November 14th, 2007

MOOTOOLS 1.2 BETA 1

The MooTools team has just released MooTools 1.2 beta 1 for public review. Some new items include:

The list of changes is WAY too long for this post so be sure to head on over to the MooTools blog for the full details.

November 14th, 2007

AXSJAX: ACCESS-ENABLING AJAX

Charles L. Chen, the developer behindhand the Fire Vox Firefox plugin that enables the covering to speech to you, has free a newborn unstoppered maker project, AxsJAX.

In my prototypal hebdomad at Google, I unconcealed Google Reader a highly optimized take reverend with rattling beatific keyboard support. For my official send at Google, I definite to access-enable this covering using W3C ARIA. Using Greasemonkey, I could dispense JavaScript cipher to add the necessary ARIA bits to attain Google Reader feature the correct things at the correct time.

Based on the undergo of access-enabling Reader, we hit today refactored the cipher to become up with a ordinary JavaScript hold for enhancing the availableness of AJAX applications. This hold is titled AxsJAX, and it was civilised in the impact of access-enabling Web Search.

We’re today agog to open-source this hold since we conceive that there is null Google-specific in the techniques we hit implemented. We elicit the Web developer accord to hold us together delimitate a burly hold for fast prototyping of availableness enhancements to Web 2.0 applications.

The knowledge to apace image end-user interaction has led to an discharge in the sort of AJAX applications; until now, visually broken users hit been mitt behindhand in this process. We wish that the AxsJAX hold encourages the Web accord to alter the noesis of Web 2.0 utilization to finding the difficulty of accessing flush Web interaction in an eyes-free environment.

What AxsJAX does…

The AxsJAX hold helps dispense availableness features into these applications so that users of reconciling technologies much as concealment readers and self-voicing browsers undergo the aforementioned take of interactivity that is today condemned for acknowledged by users of Web 2.0 applications.

AxsJAX injects availableness enhancements as circumscribed by W3C ARIA. The prerequisites for experiencing its benefits include:

  1. A recent Web covering same Firefox 2.0 or after that supports W3C ARIA.
  2. Adaptive technologies that move aright to the availableness enhancements introduced by W3C ARIA.
  3. In particular, some of the enhancements injected by AxsJAX depend on hold for springy regions a feature that enables reconciling technologies same concealment readers and self-voicing browsers care aright with anachronic updates to portions of a Web page.

The AxsJAX hold crapper dispense availableness enhancements into existing Web 2.0 applications using some of individual accepted Web techniques:

  • As a bookmarklet — diminutive snippets of JavaScript? that are utilised to create sharp bookmarks.
  • Using Greasemonkey — a coercive covering spreading that allows end-users to make the countenance and see of Web sites via bespoken scripts.
  • Using Fire Vox — Fire Vox, an unstoppered maker conversation covering spreading for Firefox, automatically injects the AxsJAX scripts if the “Use place limited enhancements” choice is overturned on.