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

indentation and anonymous function wrappers

Expand Messages
  • mbrrtt
    Most programs I write lately are wrapped in an anonymous function which is immediately executed, thus: (function () { ... the real code ... }()); As is well
    Message 1 of 4 , Aug 18, 2009
    • 0 Attachment
      Most programs I write lately are wrapped in an anonymous function
      which is immediately executed, thus:

      (function () {
      ... the real code ...
      }());

      As is well known, this avoids the creation of any unnecessary global
      variables. JSLint, however, requires the inner code to be indented
      (and I certainly wouldn't want to turn off checking for proper
      indentation in general). But this form results in almost the entire
      code being indented, which wastes space, and-- in this case-- doesn't
      add to readability or intelligibility at all.

      I'm wondering whether there could be an option to suppress this
      unnecessary indentation, or even make it the default behavior without
      an option at all. It might work like this: if the first executable
      statement in a javascript file or a script within html is a
      parenthesized anonymous function which is terminated only at the last
      executable statement of the file or script, no indentation is
      required or suggested for all the code in between.

      Or, an option in the form of a special comment might mark function
      statements whose contents don't need to be indented.

      What do you think?
    • Douglas Crockford
      ... I think you should indent your code properly.
      Message 2 of 4 , Aug 18, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "mbrrtt" <mbrrtt@...> wrote:

        > What do you think?

        I think you should indent your code properly.
      • Cheney, Edward A SSG RES USAR USARC
        ... I understand the need to use such functions for scope and containment, but what benefit do anonymous functions serve compared to named functions,
        Message 3 of 4 , Aug 19, 2009
        • 0 Attachment
          > Most programs I write lately are wrapped in an anonymous function
          > which is immediately executed, thus:
          >
          > (function () {
          > ... the real code ...
          > }());

          I understand the need to use such functions for scope and containment, but what benefit do anonymous functions serve compared to named functions, especially if they are functions of immediate invocation? I have found that it is always helpful to name immediately invoked functions to provide a reference point for more precise documentation even if that function name is never used. I have never used anonymous functions and would be curious to know if there are benefits that I am not otherwise seeing.

          You can always alter js_beautify to achieve your desired formatting for auto formatting of your code, but that will not help you with white space validation from JSLint.

          Thanks,
          Austin
        • Michael Lorton
          The world, as they say, is a very big place. I myself almost never use NAMED functions. That is, I never do this: function f() { ... }; Sometimes, not often,
          Message 4 of 4 , Aug 19, 2009
          • 0 Attachment
            The world, as they say, is a very big place. I myself almost never use NAMED functions. That is, I never do this:

            function f() { ... };

            Sometimes, not often, I do this:

            var f = function() { ... };

            which is a little different (not much -- just that the function declared does not know itself to be named "f" in the global scope).

            When I need globally-available functions, I do this:

            var module = module || {};
            module.submodule = module.submodule || {};

            $.update(module.submodule, function() {
            var anyPrivateVariable;
            var anyPrivateFunction = function() { ... };

            return {
            publicFunction0 : function() { ... },

            publicFunction1 : function() { ... }
            }
            });

            The benefits are:
            + only "module" is added to the global namespace (contaminating the global namespace is ConsideredHarmful).
            + several files can contribute other submodules to this module and other functions to this submodule
            + anyPrivateVariable and anyPrivateFunction are only available from inside the submodule (actually, to the part of the submodule in this file)
            + the public functions can be referred to as module.submodule.publicFunction0 and module.submodule.publicFunction1

            (If you don't understand the importance of the private/public distinction and of information hiding in general, you have to back up and read any good programming book written in the last 15 years.)

            But whenever I do event-handling (which, if you think about it, is MOST of what you do with Javascript) I use truly anonymous functions (that is, functions that are never even assigned to local variables, just used used as parameters to function calls).

            $.ajax(url, function(data) { ... process data ... });

            and everywhere I need a closure or code-block

            $.each(someArray, function(index, element) { ... process element ... });


            Hope this help.

            Michael







            ________________________________
            From: "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...>
            To: jslint_com@yahoogroups.com
            Sent: Wednesday, August 19, 2009 12:04:41 AM
            Subject: Re: [jslint] indentation and anonymous function wrappers

            I understand the need to use such functions for scope and containment, but what benefit do anonymous functions serve compared to named functions, especially if they are functions of immediate invocation? I have found that it is always helpful to name immediately invoked functions to provide a reference point for more precise documentation even if that function name is never used. I have never used anonymous functions and would be curious to know if there are benefits that I am not otherwise seeing.

            You can always alter js_beautify to achieve your desired formatting for auto formatting of your code, but that will not help you with white space validation from JSLint.

            Thanks,
            Austin

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