4

When I declare a function like this:

function x() { return 123 };

Then:

typeof x; //return "function"
typeof x(); //return "number"

That's ok, but once I create a variable like this:

var y = function x() { return 123 };

It becomes:

typeof y; //return "function"
typeof y(); //return "number"
typeof x; //return "undefined"
typeof x(); //return error

Why does x lose his function? Please helpp

10
  • I am getting different results..Tried in chrome and mozilla.. Commented May 17, 2015 at 8:16
  • @RayonDabre I don't, I tried both too. What exactly did you try? Commented May 17, 2015 at 8:18
  • 5
    See kangax.github.io/nfe --- under the headline "Named function expressions" this exact thing is mentioned (end of that section): "An important detail to remember is that this name is only available in the scope of a newly-defined function; specs mandate that an identifier should not be available to an enclosing scope:" Commented May 17, 2015 at 8:19
  • @Pius, check this Commented May 17, 2015 at 8:21
  • @RayonDabre That is completely different. What are you trying to show? Of course your "x()" is defined -- you do so at the top! See my comment and the link above. Commented May 17, 2015 at 8:23

1 Answer 1

1

Why does x lose his function?

A function declaration creates a variable of the same name in the current scope.

A named function expression does not (except in certain older versions of Internet Explorer, which is a bug).

That's just how function expressions are supposed to work.

Sign up to request clarification or add additional context in comments.

6 Comments

I usually say that «function x(){}» is only a shorthand for «var x = function x(){}». The first 'x' is the variable name, which is the name of the variable which holds the function in it's declaration time. The second 'x' is the actual name of the function, which is what you will see in error traces and debugging data despite the variable or method used to call it. …also, function name can be used to call itself (recursively) inside the function whithout caring for if the initial holding variable is deleted or reused.
I usually say that «function x(){}» is only a shorthand for «var x = function x(){}». — Well, it isn't. Aside from all the other differences you've already listed, function declarations are hoisted and function expressions are not.
@bitifet does it mean that whatever name I use to declare function, it will lose its function if I declare it as a variable in another name? And the function itself will belong to the new variable
True. I tryed to explain clearly why there is the two forms and did wrong. It is actually not "only" that...
@Pius — No. Assigning a function to a variable won't remove all other references to it. It's just that creating a function using a function expression won't create a variable of the same name in the current scope.
|

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.