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

"use strict" for libraries

Expand Messages
  • aseem.kishore@ymail.com
    A lot of libraries are written inside one big, anonymous wrapper function: (function () { // ... }()); As I understand from John Resig s post on use strict
    Message 1 of 2 , Sep 2, 2009
    • 0 Attachment
      A lot of libraries are written inside one big, anonymous wrapper function:

      (function () {
      // ...
      }());

      As I understand from John Resig's post on "use strict" <http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/>, a library author can place "use strict" inside that wrapper function:

      "A neat aspect of being able to define strict mode within a function is that you can now define complete JavaScript libraries in a strict manner without affecting outside code. [...] A number of libraries already use the above technique (wrapping the whole library with an anonymous self-executing function) and they will be able to take advantage of strict mode very easily."

      (function () {
      "use strict";
      // ...
      }());

      But this doesn't pass JSLint, as it expects "use strict" to be at the very top, outside the function:

      "use strict";
      (function () {
      // ...
      }());

      I'm not clear on whether this is necessarily a bad thing or not, because I don't know what exactly it means for "use strict" to be outside of a function (in the global scope): does it mean just that <script> (whether a file or inline) is in strict mode, or does it mean that all Javascript globally is in strict mode?

      If the latter, that's clearly something a library should avoid. But even the former, it prevents libraries from being combined into one file (for performance).

      What are your thoughts on this? Thanks!
    • douglascrockford
      ... It depends on the Require use strict ; option. You have it set, so JSLint expects to see use strict ; as the first line of the file. If you turn the
      Message 2 of 2 , Sep 2, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "aseem.kishore@..." <aseem.kishore@...> wrote:
        >
        > A lot of libraries are written inside one big, anonymous wrapper function:
        >
        > (function () {
        > // ...
        > }());
        >
        > As I understand from John Resig's post on "use strict" <http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/>, a library author can place "use strict" inside that wrapper function:
        >
        > "A neat aspect of being able to define strict mode within a function is that you can now define complete JavaScript libraries in a strict manner without affecting outside code. [...] A number of libraries already use the above technique (wrapping the whole library with an anonymous self-executing function) and they will be able to take advantage of strict mode very easily."
        >
        > (function () {
        > "use strict";
        > // ...
        > }());
        >
        > But this doesn't pass JSLint, as it expects "use strict" to be at the very top, outside the function:


        It depends on the Require "use strict"; option. You have it set, so JSLint expects to see "use strict"; as the first line of the file. If you turn the option off, then you can place it at the first line of a function instead.
      Your message has been successfully submitted and would be delivered to recipients shortly.