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

RE: [jslint] JSLint doesn't check if global variables are used.

Expand Messages
  • Jakob Kruse
    Yes, really. I fail to see why I would want to reference the global from within its own declaration? That said, this discussion has gotten quite a bit off
    Message 1 of 10 , May 5, 2009
    • 0 Attachment
      Yes, really. I fail to see why I would want to reference the global from within its own declaration?

      That said, this discussion has gotten quite a bit off track. The only possible purpose of declaring a global variable/function/object/whatever is so that it can be accessed globally – from everywhere, and in particular, from elsewhere. I find the current behavior of JSLint (not forcing me to declare globals twice for clarity) to be correct. Declaring a global in some file and not using it in that file (but possibly in every other file loaded) should not result in errors or warnings.

      /Jakob

      From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On Behalf Of Michael Lorton
      Sent: 5. maj 2009 18:24
      To: jslint_com@yahoogroups.com
      Subject: Re: [jslint] JSLint doesn't check if global variables are used.





      Really? Your libraries never have externally available functions or data that they themselves use?

      You could always do this, I suppose:

      var myModule = ( function() {
      var utilityFunction = function() { ... };
      return {
      utilityFunction : utilityFunction,
      someOtherFunction : function() {
      ...
      utilityFunction();
      ....
      }
      }
      })();

      That is, you could have an internal reference for each function (or datum) that is only reachable through the closure and an external reference the library itself never uses -- but why you would do so, other than to trigger a misleading global myModule not used error, I don't know why you would. Am I missing something?

      M.

      ________________________________
      From: Jakob Kruse <kruse@...>
      To: jslint_com@yahoogroups.com
      Sent: Tuesday, May 5, 2009 9:11:46 AM
      Subject: RE: [jslint] JSLint doesn't check if global variables are used.

      I disagree. Not with the ”single object” approach, that is a very good practice, but that libraries usually refer to themselves. I have a multitude of “classes” in the various projects I work on – they are all a single js file which declares a single global variable, and not a single one of them refers to itself by name. Why should it?

      In the case of general utility libraries such as Prototype or jQuery then yes, they probably all refer to themselves, but they are the exception, not the norm.

      /Jakob

      From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On Behalf Of Michael Lorton
      Sent: 2. maj 2009 07:01
      To: jslint_com@yahoogroups.com
      Subject: Re: [jslint] JSLint doesn't check if global variables are used.

      You're right, of course, but even a library should avoid polluting the global names space any more than necessary. I prefer to have all the library's functions wrapped up in a single object, e.g.:

      var MyLib = {
      myFirstFunction : function () { ...},
      mySecondFunction : function () { ...}, ,,,

      };

      Usually, the library will refer to itself at least once, obviating need there for an /*external */ tag.

      M.

      ________________________________
      From: Jakob Kruse <kruse@...>
      To: jslint_com@yahoogroups.com
      Sent: Friday, May 1, 2009 1:24:47 PM
      Subject: RE: [jslint] JSLint doesn't check if global variables are used.

      Actually, that is very common. The purpose of most libraries is to declare global variables for use elsewhere.

      /Jakob

      From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On Behalf Of Michael Lorton
      Sent: 1. maj 2009 18:47
      To: jslint_com@yahoogroups.com
      Subject: Re: [jslint] JSLint doesn't check if global variables are used.

      The /*global */ tag solves the reverse problem: how tell if a variable used here is declared elsewhere.

      Perhaps we need a /*external */ tag that asserts a variable declared here is used elsewhere. Personally, I would oppose it (mildly): if you aren't using it here, why are you declaring it here, hmmm?

      M.

      ________________________________
      From: pauanyu <pcxunlimited@...>
      To: jslint_com@yahoogroups.com
      Sent: Friday, May 1, 2009 9:35:54 AM
      Subject: Re: [jslint] JSLint doesn't check if global variables are used.

      --- In jslint_com@yahoogroups.com, Michael Lorton <mlorton@...> wrote:
      >
      > In a formal sense, it's a bug, but how could it be fixed? A global variable might be used by a different script, or by a bit of Javascript in an event handler, even by Flash. There's no way to tell if one is never used.
      >
      > Another reason that Global Variables Suck.
      >
      > M.

      As to your first point.. JSLint supports the /*global */ construct for global variables defined elsewhere.

      ------------------------------------

      Yahoo! Groups Links

      [Non-text portions of this message have been removed]

      [Non-text portions of this message have been removed]

      ------------------------------------

      Yahoo! Groups Links

      [Non-text portions of this message have been removed]

      [Non-text portions of this message have been removed]

      ------------------------------------

      Yahoo! Groups Links

      [Non-text portions of this message have been removed]


      [Non-text portions of this message have been removed]
    • pauanyu
      ... I agree, but the idea was to be more of a helpful hint. If you declare a variable but don t use it, JSLint doesn t throw an error. Instead, it lists the
      Message 2 of 10 , May 5, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@...> wrote:
        >
        > Yes, really. I fail to see why I would want to reference the global from within its own declaration?
        >
        > That said, this discussion has gotten quite a bit off track. The only possible purpose of declaring a global variable/function/object/whatever is so that it can be accessed globally â€" from everywhere, and in particular, from elsewhere. I find the current behavior of JSLint (not forcing me to declare globals twice for clarity) to be correct. Declaring a global in some file and not using it in that file (but possibly in every other file loaded) should not result in errors or warnings.
        >
        > /Jakob
        >

        I agree, but the idea was to be more of a helpful hint. If you declare a variable but don't use it, JSLint doesn't throw an error. Instead, it lists the Unused variables in the useful summary at the bottom.

        Although it would be great to have this same helpful hint with global variables, it's been shown in this thread that such a system would almost certainly be needlessly complex, without very much gain.
      Your message has been successfully submitted and would be delivered to recipients shortly.