Suggestions for creating this Javascript sexier code snippet?

I am trying to make a way to add functions under a specific namespace (dpcom). This may be in dotted notation depending on what the user has done in his javascript file. So basically, what I want them to do is something like this:

dpcom.library('something.foo.funcName', function() { // Code goes here. }) 

Then they can later call their things the following:

 dpcom.something.foo.funcName(); 

What will execute the code that they defined above. The code in which I want to help make it sexier is here (it uses jQuery):

 dpcom.library = function(name, func) { root = dpcom; objects = name.split('.'); lastElement = objects[objects.length - 1]; $(objects).each(function(idx, elem) { if (elem == lastElement) { root[elem] = func; } else if (!root[elem]) { root[elem] = {} } root = root[elem]; }); } 

This should handle the possible dotted notation and create objects inside my namespace if they don't already exist (I don't want to overwrite any already declared objects).

The code that I have above seems to work fine, but I have a feeling that I can do it better, but my brain is not telling me where ... Does anyone want to hit it?

+4
source share
6 answers

You should be able to do this in a slightly more attractive way using shift () :

 dpcom.library = function(name, func) { var root = dpcom, objects = name.split('.'), elem; while (elem = objects.shift()) { if (objects.length) { if (!root[elem]) root[elem] = {}; root = root[elem]; } else root[elem] = func; } } 

This way you can disable the jQuery requirement. Also, be sure to explicitly declare your var with the var keyword.

Depending on your definition of the sexual code, you can also replace this block:

  if (objects.length) { if (!root[elem]) root[elem] = {}; root = root[elem]; } 

with:

  if (objects.length) root = !root[elem] ? root[elem] = {} : root[elem]; 
+3
source

Is this some weird code golf?

Try it, it might work ...

 dpcom.library = function(name, func) { var p = name.split('.'), e, root = this; while (e = p.shift()) root = root[e] = root[e] || (!p.length ? func : {}); } 
+3
source

Here is a hit on him

 dpcom.library = function(name, func) { var lastElement, root = dpcom, objects = name.split(/\./); lastElement = objects[objects.length - 1]; $.each(objects, function(idx, elem) { if (!root[elem]) { if (elem == lastElement) { root[elem] = func; } else { root[elem] = {} root = root[elem]; } } }); } 

EDIT:

I found myself using the jQuery extension method. Check out the docs here .

+1
source

Using the reduce method for arrays,

 dpcom.library = function(key, value) { var objects = key.split('.'); var leaf = objects.pop(); var constructHierarchy = function(object, property) { return (object[property] = object[property] || {}); }; objects.reduce(constructHierarchy, dpcom)[leaf] = value; }; 

To increase the coefficient of sexuality, make the second value general, and not just a function. This is already being done in all answers, but an explicit indication of this helps :)

 dpcom.library('awesome.object', { foo: 'woah' }); 
+1
source

I donโ€™t know if it is sexier or more readable, but it doesnโ€™t need da Query and it doesnโ€™t have global globals;)

 var dpcom = {'test': null, 'something': {}}; dpcom.library = function(name, func) { for(var i = 0, node = this, names = name.split('.'), l = names.length; i < l; i++) { node = node[names[i]] = ((i == l - 1) ? func : node[names[i]] || {}); } }; dpcom.library('something.foo.funcName', function() { // Code goes here. }); 

Result:

 { test: null , library: [Function] , something: { foo: { funcName: [Function] } } } 
0
source

Check out the YUI 2 namespace function:

 YAHOO.namespace('myNS').funcName = function(){/* do something with arguments */}; 
0
source

All Articles