4 ms·
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 optio
by warfangle 17y ago
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'); };
- nkohari 17y agoI 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");
- yannis 17y agoClever! How do you add your properties and methods? Another clever trick?
- woadwarrior01 17y agoYAHOO.namespace in YUI does the same thing, except that it creates everything under the YAHOO namespace.
- willwagner 17y agoI 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"; }
- yannis 17y agoThanks 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 } }