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

I don't like using var in that way, because it seems like you're declaring two distinct variables. In your example, I'd much prefer:

   function make_dot_product(dim) {
      var dot;
      if (dim === 3) {
         dot = function(a, b) {
            return a.x * b.x + a.y * b.y + a.z * b.z;
         };
      } else if (dim === 2) {
         dot = function(a, b) {
            return a.x * b.x + a.y * b.y;
         };
      }

      return dot;
   }
This also makes it clearer what happens if neither dim is neither 2 nor 3.


The variables are the same. Var statements get hoisted to the beginning of the function.

That said, it is cleaner to only 'var' each variable once, to suppress this confusion.




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

Search: