Loading ...
Sorry, an error occurred while loading the content.

1393[jslint] Re: Implied global or hoisted var

Expand Messages
  • bertbelder
    Jul 28 5:44 AM
    • 0 Attachment
      I know that reversing the declaration order would fix the example code I posted. However the situation that caused to me run into this doesn't really allow reversing declaration orders without making the code really unreadable. Let me explain.

      Suppose you have to load several pieces of data *asynchronously* (using ajax for example) where the next data source to be loaded depends on data loaded before. Let's say the function `ajax(url, callback)` is defined, that does an ajax request and calls the callback when the response arrives.
      Then you could write:

      var whatWereLookingFor;
      ajax('/some/url', function(result) {
      ...
      ajax('/some/other/' + result.x, function(result) {
      ...
      ajax('/xyz/' + result.bar, function(result) {
      ...
      ajax('/bar/' + result.id, function(result) {
      // Woohoo we got it
      whatWereLookingFor = result;
      });
      });
      });
      });

      But that's terribly readable. So I was trying to write:

      function loadSome() {
      ajax("/some/url", loadOther);
      }
      function loadOther(result) {
      ajax("/some/other/" + result.x, loadFoo);
      }
      function loadFoo(result) {
      ajax("/xyz/" + result.bar, loadBar);
      }
      function loadBar(result) {
      ...
      }

      But JSLint doesn't eat that.

      The suggestion to reverse declaration order would solve the JSLint error, but then the function that is called last would be defined first, which I think would make the code confusing to a reader.

      The other solution would be to put a statement like
      var loadSome, loadOther, loadFoo, loadBar at the start of the function but that clutters my code with unnecessary statements, besides requiring me to keep the var statement in sync with the functions actually defined.

      So, any suggestions here?

      I don't really mind that JSLint complains when encountering a function call before the function is defined. But I would be very happy if it would just say that, and not mess up the implied globals list with variables that are NOT global.

      - Bert
    • Show all 10 messages in this topic