Hacker News new | ask | show | jobs
by njonsson 5289 days ago
All decent suggestions. I quibble with this, though:

  // Don't do this:
  function getData() { }
  // Do this instead:
  var getData = function() { };
The assignment is righteous, but by omitting the function’s name, you make stack traces more difficult to follow. Better this instead:

  var getData = function getData() { };
3 comments

I definitely agree with your point about stack traces - I've used var x = function(){} for some projects, but have found in practice that named functions are very much worthwhile when dealing with stack traces?

What's the benefit of var x = function x(){}? Is it just illustration that functions can be assigned to variables?

I have to disagree. Part of the reason is that it teaches the concept of anonymous functions.

The main reason, though, is the anonymous function case makes it more apparent that the function is an object.

Separating the RHS from the LHS is significant. The right side creates the object, the left side binds it to the reference. Much like `var a = 3`.

It is the assignment that makes it interesting for people that I teach JavaScript. Consider the following, can you do that in a less function-oriented language like Java easily?

     var getData = function () {};


     var mygetdata = getdata;

     getData("foo")

     mygetData("foo")
Yeah, I didn't get this either. What's wrong with:

    function getData() {}
    var getData = new getData();