You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by Dan Dumont <dd...@us.ibm.com> on 2011/11/29 20:42:47 UTC
Named function expressions in shindig; declaring functions; best practices
I was wondering if we had any document about best practices for this kind
of thing in shindig.
I've seen a bunch of different ways of creating temporary or permanent
functions throughout the codebase.
This is an excellent write up on the subject:
http://kangax.github.com/nfe/
Apart from function declarations, named function expressions don't seem to
popular in shindig:
(ex: var gadgets.io.foo = function gadgets_io_foo(){}; )
This could help debugging. I'm not sure it's necessary and it looks to
have implications on memory consumption.
Usually when I write scoped utility functions, I do it like this:
var foo = (function() {
var car = function() {
// do something;
};
return {
bar: function() {
var ret = car();
// use ret somehow
return ret;
}
}
})();
foo.bar(); // etc...
This does not appear to have any problems according to this write up,
unless you count the stack trace names being barren as a problem.
This makes things a little better:
var foo = (function() {
function car() {
// do something;
};
return {
bar: function() {
var ret = car();
// use ret somehow
return ret;
}
}
})();
At least car will be named in the stack trace, and because it's in a
closure (important!) it's name won't be leaked into the global namespace.
Naming bar introduces a memory problem, according to the article:
var foo = (function() {
function car() {
// do something;
};
return {
bar: function foo_bar() {
var ret = car();
// use ret somehow
return ret;
}
}
})();
The foo_bar declaration gets trapped in the closure and won't be garbage
collected unless you do something like:
var foo = (function() {
function car() {
// do something;
};
var ret = {
bar: function foo_bar() {
var ret = car();
// use ret somehow
return ret;
}
}
foo_bar = null;
return ret;
})();
Anyway... interesting food for thought. If anyone knows what
closure-complier does to the named function expressions, I'd be interested
in learning.
Does anyone care about naming function expressions?