3

I'm just trying to get my head around this function expression.

It seems that if I create a function expression (p) that seems to contain a function declaration, the function declaration a() returns undefined.

var p;
p = function a() { return 'Hello' }

typeof p; // returns 'function'
typeof a; // returns 'undefined'

Can anyone explain why this is the case?

And also please let me know if my terminology is off too.

5
  • Is there a reason why you want to do this? Use function a() { return "Hello" } var p = a; if you need both, or don't specify the anonymous function with a name. Commented Apr 29, 2013 at 20:59
  • @Ian: "...or don't specify the anonymous function with a name..." Um... ;-) Commented Apr 29, 2013 at 21:08
  • @T.J.Crowder What? Other than the function being able to access itself, I don't see a use for giving the anonymous function a name...it's clearly confusing the OP too! Commented Apr 29, 2013 at 21:14
  • 1
    @Ian: I was just pointing out that if it has a name, it's not anonymous, so saying "...don't give the anonymous function a name..." is oxymoronic. :-) Commented Apr 29, 2013 at 21:20
  • 1
    @T.J.Crowder Haha yeah I see I see. Poor wording on my part. I'm just so used to never giving a function expression a name. Commented Apr 30, 2013 at 13:57

3 Answers 3

5

It isn't a function declaration. It is a function expression that happens to have a name. The name does not create a variable, but you can see it on the object

quentin@raston ~ $ node
> var p;
undefined
> p = function a() { return 'Hello' }
[Function: a]
> typeof p; // returns 'function'
'function'
> typeof a; // returns 'undefined'
'undefined'
> p
[Function: a]
> p.name
'a'
>
Sign up to request clarification or add additional context in comments.

2 Comments

Worth noting: the function name will also be available as an identifier inside the function body, so it's possible to call a named function expression recursively.
Also worth noting that the name property of function instances is non-standard.
1

It seems that if I create a function expression (p) that seems to contain a function declaration

No. It is a named function expression, which does not "contain" a function declaration. The name of the function expression is available as an identifier inside the function's scope (pointing to the function itself), and as the nonstandard name property.

1 Comment

(Can't believe nobody brought up the link to kangax' site)
-3

You can think of it as an anonymous function.

The reason that this is valid is because the local function name a can be used within the function declaration for recursion, but is not valid outside of this scope.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/function

2 Comments

"You have created an anonymous function declaration." No, he/she hasn't. First, it's not anonymous. Second, it's not a declaration. It's a named function expression. (Function declarations and function expressions are different things; JavaScript has both. You know it's an expression if it's used as a right-hand value.) You're quite correct that the a is in scope within the function, but not outside it, though. (Not my downvote, btw.)
Sorry, my terminology might be off - I was aiming to get across the gist of the problem. How would you describe it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.