Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The first one will be hoisted to the top of the function (the whole declaration), the second one won't (the variable will, but not the function expression itself).

EDIT: To clarify:

All variable declarations in Javascript do indeed get hoisted to the top of the scope, but with function declarations even the function value itself gets hoisted. So for instance, this returns 'two':

  function foo() {
    function one() { return 'one'; }
    return one();
    function one() { return 'two'; }
  }

  foo();

But this returns 'one':

  function foo() {
    var one = function one() { return 'one'; }
    return one();
    var one = function one() { return 'two'; }
  }

  foo();


What? I have no idea what you are talking about. Both get hoisted to the top of closure scope...


See my edit.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: