0

I don't understand a part of the accpeted answer of this OP:

Javascript function scoping and hoisting

Writer says:

"

Also, in this instance,

function a() {}

behaved the same as

var a = function () {};

".

What I know is that function expression is different than function declaration, at least for hoisting. Why are they similar in this case?

Community
  • 1
  • 1
Adib Aroui
  • 4,981
  • 5
  • 42
  • 94

2 Answers2

1

The difference is that the first line is defined at run-time, whereas second line is defined at parse-time for a script block.

Look at the full answer on stack, here : var functionName = function() {} vs function functionName() {}

Community
  • 1
  • 1
Jitrixis
  • 74
  • 8
  • 4
    The accepted answer on the linked question is really quite wrong. Strictly speaking, *neither* function is "defined" at parse time. – Pointy Jul 29 '15 at 23:44
  • 1
    It would be best to flag the post as a duplicate instead of copying part of the answer here. – Anonymous Jul 29 '15 at 23:46
  • @anonymous : I actually can't access this option. – Jitrixis Jul 29 '15 at 23:49
  • Oh, true. Still, I would be careful about doing this even without that ability. Maybe just move on to a different question to gain reputation first. But, there's not much point in doing that now, so no worries. – Anonymous Jul 29 '15 at 23:52
  • @anonymous : Ok, I understand. – Jitrixis Jul 29 '15 at 23:55
  • @Pointy—I've given up trying to point out such subtleties of program execution, e.g. "hoisting" is a misnomer. To me, program execution is described clearly in [*ES5*](http://www.ecma-international.org/ecma-262/5.1/index.html#sec-10.4), the equivalent in [*ed 6*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-executable-code-and-execution-contexts) seems more complex and less comprehensible. – RobG Jul 30 '15 at 00:18
  • thank you for this answer , even I don't fully understand , link and comments will be of help for me to learn more. Please do you have any link or doc on these notions of run-time and parse time for javascript? thanks – Adib Aroui Jul 30 '15 at 00:30
  • @RobG sometimes I think the ES6 spec was designed as a level-up challenge since the ES5 spec was getting read by more and more people. – Pointy Jul 30 '15 at 02:37
1

Why are they similar in this case?

Because var is hoisted (but not set), like a function declaration is hoisted, meaning that there is an a in the local scope before a = 10; is evaluated, so the global a never gets modified - the identifier lookup finds the local a first so sets that


Related parts of the other question

var a = 1;

function b() {
    a = 10;
    return;

    function a() {}
}
b();
alert(a);

Why is a === 1?


That the answer was trying to say was that b is equal to

function b() {
    function a() {}
    a = 10;
    return;
}

Similar to

function b() {
    var a = function () {};
    a = 10;
    return;
}

i.e. there is an identifier a defined in b, so

function b() {
    var a = 10;
    return;
}

And now, obviously, the global a will not be modified by b


Please note that the position of the var isn't really important, just that it's there, the following will produce the same behaviour

function b() {
    a = 10;
    return;
    var a;
}
Paul S.
  • 64,864
  • 9
  • 122
  • 138