code-guide icon indicating copy to clipboard operation
code-guide copied to clipboard

Functions: Closures

Open jaymcp opened this issue 11 years ago • 9 comments

I've just seen something on this, and it seems like a cool idea; going to have to try testing it. Apparently one can have named closures:

var a = [a, b, c];
a.forEach(function loopy (v, i) {
  // oooh, fancy
});

jaymcp avatar Jul 28 '14 13:07 jaymcp

No. :-1:

ConnorVG avatar Jul 28 '14 13:07 ConnorVG

why not; it supposedly makes stack traces easier (not just Anonymous function)?

jaymcp avatar Jul 28 '14 14:07 jaymcp

It's an anonymous function - not a named function. So... Why name it? It looks gross as hell and has no benefit. The stack trace would still point to the function, whether it was named or not afaik.

ConnorVG avatar Jul 28 '14 14:07 ConnorVG

Have you never seen a stack trace in console that's just a bunch of anonymous functions? Naming them (they're still anonymous & discarded, so more like temporary aliases) could be helpful in that scenario (especially if you're in the situation where they end up chaining). As I said, though, it's not something I've yet tested - just a suggestion I saw somewhere. Does look a bit ugly, but I'm all for a bit of ugly if it's helpful. I'll run some tests in a bit.

jaymcp avatar Jul 28 '14 14:07 jaymcp

I'm not at all, line number takes you right there. Annonymous function caller, simple. This is the ugliest thing ever :sob:

ConnorVG avatar Jul 28 '14 14:07 ConnorVG

also, what about collisions? Is it collidable?

ConnorVG avatar Jul 28 '14 14:07 ConnorVG

Lets see what the test is like. Have you got the link where you saw about it?

markgoodyear avatar Jul 28 '14 14:07 markgoodyear

Just think of this:

angular.module('app')
  .controller('AccountController', [ '$scope', '$state', function AccountController ($scope, $state) {
    $scope.stateData = $state.current.data;

    // ..
  }]);

ConnorVG avatar Jul 28 '14 14:07 ConnorVG

here's the link (took me a while to find it again): https://github.com/felixge/node-style-guide#name-your-closures I've not run any tests yet

jaymcp avatar Jul 28 '14 15:07 jaymcp