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

Re: [jslint] indentation and anonymous function wrappers

Expand Messages
  • 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 1 of 4 , Aug 19, 2009
      > 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 2 of 4 , Aug 19, 2009
        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.