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

An additional wrinkle: recursive functions. If you've got a recursive function, you absolutely must use a function declaration to introduce the function identifier. Otherwise you inadvertently create a closure. Consider:

  var a = function(b) {
    if (b === 0) {
      return true;
    }
    return a(--b);
  };
  var b = a;
  a = function() { return false; };
  b(5);
As an alternative, you should go

  var a = function a2(b) {
    if (b === 0) {
      return true;
    }
    return a2(--b);
  };
  var b = a;
  a = function() { return false; };
  var a2 = function() { return false; };
  b(5);
Perhaps counterintuitively, a2 doesn't leak into the containing scope, which makes it safe to use within the function it declares.

Some older browsers fuck this up though, IIRC.



you can use the same name for both the variable and the function in your (alternative) solution. It's better and less confusing. and another reason your alternative is preferred is because arguments.callee is going away (if it isn't gone already) :/


Indeed. Having separate names was for didactic purposes. In real code, naming the variable the same as the function would be very strongly recommended.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: