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

Re: [jslint] Re: /*global */

Expand Messages
  • Jakob Kruse
    200-500 javascript files in an application is common where I work. Most of those files use some JavaScript library, for instance Prototype. All JavaScript
    Message 1 of 34 , Jun 1, 2009
    • 0 Attachment
      200-500 javascript files in an application is common where I work. Most of those files use some JavaScript library, for instance Prototype. All JavaScript libraries define one or more globals.

      Putting a /*global $ */ at the top of every one of those several hundred files is tedious, but not without informative value (especially if combined with "// Prototype"), and it is automatically removed when I minify my code for deployment.

      Putting "use strict" and/or var $ at the top of every one of them does absolutely nothing for me except bloat my application and make it slower. As such it is unacceptable.

      Also, in general there will be somewhat considerable resistance towards moving to a version of JSLint which will automatically flag every single existing (and previously valid) JavaScript file as invalid. I think we would rather stay with an older version for some time and then stop using it altogether.

      Also, in response to Douglas' mail offering an explanation (just came in while I wrote this):

      There's a major difference here. I suspect JSLint failed to run because JSLint itself was declared as "JSLINT = ..." and not "var JSLINT = ...". That would obviously be a mistake (I did remember seeing a recommendation to skip var for globals earlier, and I never bought that one either). I find that case completely different from having to add a "var JSLINT;" to every other JavaScript file on the planet that uses JSLINT.

      Surely JSLint can check that globals are declared/defined using "var my_global = ..." without needing additional "var my_global" statement all over my code?

      In general I'm all for JSLint, and I've changed my programming style considerably to please it (and benefitted from it), but this is not ok.

      The var solution even seems to me to offer greater potential of masking errors than /*global */. If I forget to include the file that actually defines the global, the /*global */ solution will offer a reasonable error ("my_global is not defined"). If I var'ed it at the top there will probably be an error, but not always ("var x;" is identical to "var x = undefined;" so the global will have a value), and even if there is an error, it will make a lot less sense.

      Please reconsider!

      /Jakob




      From: Michael Lorton [mailto:mlorton@...]
      To: jslint_com@yahoogroups.com
      Sent: Mon, 01 Jun 2009 19:51:55 +0200
      Subject: Re: [jslint] Re: /*global */




      While in general I approve of being able to turn off warnings, why do you have "possibly several hundred "var $;" statements"? You would need at most one per file and you only have "several" files.

      An alternative to turning off the warning would be to mark every such declaration (e.g. "var $; /* extern */") and then during the minification process, deleting all those lines. You'd get the advantage of being warned about accidental globals without the slow-down in loading.

      M.

      ________________________________
      From: Jakob Kruse <kruse@...>
      To: jslint_com@yahoogroups.com
      Sent: Monday, June 1, 2009 10:23:35 AM
      Subject: Re: [jslint] Re: /*global */

      I also find removing the support for /*global */ to be a major setback.

      Combining several JavaScript files into one for deployment is somewhat a must. In most of the applications I work on such a file would include possibly several hundred "var $;" statements. That's obviously unacceptable, so we would effectively have to stop using JSLint if this was enforced. If it was an option we would always turn it off, regardless of the reasoning behind it.

      Please keep support for /*global */.

      Just my two cents.

      /Jakob

      From: pauanyu [mailto:pcxunlimited@...]
      To: jslint_com@yahoogroups.com
      Sent: Mon, 01 Jun 2009 18:39:57 +0200
      Subject: [jslint] Re: /*global */

      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
      >
      > I will be removing the /*global */ declarative comment. I am persuaded that it is best to explicitly use var to declare all global variables, even when accessing variables created by other compilation units.
      >
      > /*global */ will just be a comment.
      >

      In answer to people's concerns: I believe the primary reason for this change is that "use strict" mode does not allow implied globals:
      http://tech.groups.yahoo.com/group/jslint_com/message/540

      Now, rather than writing:
      /*global varName */

      You simply write:
      var varName;

      Disclaimer: I am not Douglas Crockford, so this is merely my own personal speculation on the subject.
    • Douglas Crockford
      ... Which do you think is more likely, that a program wants to change the global name, or that a var name declaration in a function was forgotten? We won t get
      Message 34 of 34 , Jun 12, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
        > "name" needs to be writable. It has no value by default,
        > but you can assign a string to it, indicating the name of
        > the window/tab.

        Which do you think is more likely, that a program wants to change the global name, or that a var name declaration in a function was forgotten? We won't get an implied global warning, but at least we can get a read only warning. I think that is the more useful default.
      Your message has been successfully submitted and would be delivered to recipients shortly.