Named function is better, for hoisting, debugging, and recursion.

## hoisting

It's hoisted as a function, not as "undefined".

```
console.log(typeof unnamed);
console.log(typeof named);

var unnamed = function (){};

function named() {}
```

## debugging

The stack traces generated in V8 once upon a time would not infer the name
of a function that was declared anonymously.  Now they do, which is great.
 But there are other tools out there (debuggers and such) that can trip
over such code and show it as the less than useful "(anonymous)" which
sucks when there are dozens of them.

## recursion (in strict mode)

The name of a function is a read-only property and cannot be overwritten or
changed, so it's extremely reliable.  It allows you to reference the
function without using arguments.callee

```
setTimeout(function foo() {
  if (readyYet) doStuff();
  else setTimeout(foo, 100);
}, 100);
```

vs:

```
var foo = function() {
  if (readyYet) doStuff();
  else setTimeout(foo, 100);
}
setTimeout(foo, 100);
// then later...
foo = 'bar' // boom!
```


All relatively minor things.  You're not a bad person if you do `var x =
function(){}`.  But I personally don't prefer it for those reasons.



On Fri, Dec 6, 2013 at 8:33 AM, Reza Razavipour
<[email protected]>wrote:

> I see both style and trying to understand the differences... Debugging,
> logging, other plus or minus...
>
> Option one:
>
> var zero = function (n){
>         return n < 10 ? "0" + n : n;
> };
>
> Option two:
>
> function zero (n){
>         return n < 10 ? "0" + n : n;
> };
>
> What is the "recommended" style and why?
>
>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to