News for hackers. Stuff that matters.
Re:Advanced JavaScript Techniques by warfangle on Friday October 09, 2009 @01:03PM (#871904)
I take issue with his object literal example for function arguments. My rule of thumb is: if it's a required parameter, list it in the parameters. If it's optional, put it in an object literal argument - but DOCUMENT it in a comment at the head of the function. Using a lot of object literals as arguments can get very hairy very quickly if you don't keep track of them, and keeps your code from being self-documenting. By separating out your required and your optional parameters, you can easily see which are which.

In his namespace example, also, he uses the new Object() notation, which is not nearly as concise as var foo = {}, which does the same exact thing (similarly, var arr = [] is better than var arr = new Array()).

We could rewrite the entire namespace initialization block as:

var MY = (MY) ? MY : { CUSTOM : {} };

This sets the variable MY to itself if it exists, otherwise assign an object literal that has a member property CUSTOM that happens to be an empty object. You could now create a new function, say, MY.CUSTOM.foo = function() { alert('foo'); };

Re:Advanced JavaScript Techniques by nkohari on Friday October 09, 2009 @01:21PM (#871940)
I use this function to create namespaces:

  var namespace = function(ns) {
    var scope = window;
    var tokens = ns.split(".");
    for (var idx = 0, len = tokens.length; idx < len; idx++) {
      var token = tokens[idx];
      scope = scope[token] = scope[token] || {};
    }
  };
For example: namespace("Zen.Ui");
Re:Advanced JavaScript Techniques by woadwarrior01 on Friday October 09, 2009 @06:20PM (#872584)
YAHOO.namespace in YUI does the same thing, except that it creates everything under the YAHOO namespace.
Re:Advanced JavaScript Techniques by yannis on Friday October 09, 2009 @03:15PM (#872235)
Clever! How do you add your properties and methods? Another clever trick?
Re:Advanced JavaScript Techniques by yannis on Friday October 09, 2009 @02:45PM (#872166)
Thanks for sharing your rule of thumb. I have observed that inevitably most objects/functions require three 'groups' of variables, more or less I use this pattern and I would appreciate your thoughts.

   - Defaults: Must be defined within the object literal
   - User Options : part of the function arguments 
                    (user can use an object literal,
                     anon function etc.) 
   - Settings : The actual mixin used by the methods

 function aProblem(arg1, arg2, options){ 
    // define the necessary defaults
    var defaults={} //define defaults
    // use a mixin algo to combine options and
    // defaults
    var settings = mixin(options, defaults); 
    // main function routines 
    var myObj={};

    return myObj={   
      property1 : settings.property1,
      property2 : settings.property2
    }
 }
Re:Advanced JavaScript Techniques by willwagner on Friday October 09, 2009 @02:21PM (#872113)
I use a similar technique when the number of optional arguments get out of control (i.e. if there is more than one optional argument). If you want to tight control, you can take advantage of the fact that a variable that is a function, the length member specifies the number of arguments declared as in:

(function(a,b,c){}).length == 3

Using that knowledge, you can do this at the top of a function:

if(arguments.callee.length > arguments.length){ throw "Required Parameter Missing"; }