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

Re: [jslint] Re: Implied global or hoisted var

Expand Messages
  • Michael
    ... Bert; You re making a bunch of assumptions about your code. In the future you might try to make some changes that will make your code wrong. Imagine in
    Message 1 of 10 , Jul 28, 2010
    • 0 Attachment
      On Wed, Jul 28, 2010 at 8:44 AM, bertbelder <bertbelder@...> wrote:

      >
      >
      > 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
      >
      Bert;

      You're making a bunch of assumptions about your code. In the future you
      might try to make some changes that will make your code wrong. Imagine in
      your example:

      (function outer() {
      var foo = function() {
      bar();
      };

      if (true) {
      return;
      }

      var bar = function() {
      };

      foo();
      })();

      Suddenly your variable does become global. JSLint recommends you define all
      your variables at the top of each scope, so this kind of thing won't happen.
      JSLint will hurt your feelings, but it makes your code better.


      [Non-text portions of this message have been removed]
    • bertbelder
      ... Erm, I don t see it. What becomes global then?
      Message 2 of 10 , Jul 28, 2010
      • 0 Attachment
        > You're making a bunch of assumptions about your code. In the future you
        > might try to make some changes that will make your code wrong. Imagine in
        > your example:
        >
        > (function outer() {
        > var foo = function() {
        > bar();
        > };
        >
        > if (true) {
        > return;
        > }
        >
        > var bar = function() {
        > };
        >
        > foo();
        > })();
        >
        > Suddenly your variable does become global. JSLint recommends you define all
        > your variables at the top of each scope, so this kind of thing won't happen.

        Erm, I don't see it. What becomes global then?
      • Michael
        ... beyond confusing if you have it also defined in the global scope. [Non-text portions of this message have been removed]
        Message 3 of 10 , Jul 28, 2010
        • 0 Attachment
          On Wed, Jul 28, 2010 at 9:07 AM, bertbelder <bertbelder@...> wrote:

          >
          >
          > > You're making a bunch of assumptions about your code. In the future you
          > > might try to make some changes that will make your code wrong. Imagine in
          > > your example:
          > >
          > > (function outer() {
          > > var foo = function() {
          > > bar();
          > > };
          > >
          > > if (true) {
          > > return;
          > > }
          > >
          > > var bar = function() {
          > > };
          > >
          > > foo();
          > > })();
          > >
          > > Suddenly your variable does become global. JSLint recommends you define
          > all
          > > your variables at the top of each scope, so this kind of thing won't
          > happen.
          >
          > Erm, I don't see it. What becomes global then?
          >
          > You're right, I need more coffee. It becomes undefined due to hoisting, but
          beyond confusing if you have it also defined in the global scope.


          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.