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

Re: [jslint] Re: function() -> function ()

Expand Messages
  • Michael Mikowski
    $0.02: First, the good parts advocates the following form, as it illustrates the true nature of functions: 1. var fnOnClick = function (){ ... }; Others
    Message 1 of 19 , May 2, 2010
    • 0 Attachment
      $0.02:

      First, "the good parts" advocates the following form, as it illustrates the true nature of functions:

      1. var fnOnClick = function (){ ... };

      Others advocate you should always name your functions for the benefit of stack traces. I think this is what Mr. Lorton's test code illustrates:

      2. function fnOnClick (){ ... }

      One can combine the two, but its almost certainly bad practice because keeping two names in sync is invites mismatch errors:

      3. var fnOnClick = function fnOnClick (){ ... };

      Method (1) works fine with stack traces in the most current versions of Firebug, so appears to be the best solution for my purposes. IIRC, that was not the case until recently.

      Cheers, Mike






      ________________________________
      From: Michael Lorton <mlorton@...>
      To: jslint_com@yahoogroups.com
      Sent: Sun, May 2, 2010 3:07:45 PM
      Subject: Re: [jslint] Re: function() -> function ()


      Identical? Have you ever heard the expression "not always right, but never in doubt"?

      What should the following show?

      var f = function () {
      };

      function g() {
      }
      alert(f.name == g.name);

      By any ordinary sense "identical", you'd think it would pop-up "true", but lo, f.name is undefined but g.name is "g".

      The function pointed to by f is anonymous in the sense that it does not know its own name, although other functions may have a name for it (indeed, it any function does not appear somewhere in the namespace, it's unreferenced and so, can never be called and will be cleaned up by the GC.

      M.

      ____________ _________ _________ __
      From: "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@ us.army.mil>
      To: jslint_com@yahoogro ups.com
      Sent: Sun, May 2, 2010 2:56:23 PM
      Subject: Re: [jslint] Re: function() -> function ()

      That is not an anonymous function.

      The follow two are identical.

      var f = function () {
      // body
      };

      function f () {
      // body
      }

      In both cases you are assigning a function to a named variable in a given namespace. The biggest difference is the semicolon at the end of the first example, but for your consideration the difference is whether the namespace assignment is implicit to a command or explicitly stated. An anonymous function is a function not assigned to a name. This is an example of an anonymous function:

      errorp.sort( function (a, b) {
      return a - b;
      });

      Austin

      ----- Original Message -----
      From: Douglas Crockford <douglas@crockford. com>
      Date: Saturday, May 1, 2010 23:53
      Subject: [jslint] Re: function() -> function ()
      To: jslint_com@yahoogro ups.com

      > --- In jslint_com@yahoogro ups.com, Rob Richardson < wrote:
      > >
      > > JSLint reports that anonymous functions such as this:
      > >
      > > var f = function(args) {
      > > // body
      > > }
      > >
      > > would be better like this:
      > >
      > > var f = function (args) {
      > > // body
      > > }
      > >
      > > Why is the space between 'function' and '(' preferable?
      >
      > It is for readability. It makes it clearer that you are creating a function and not calling a function.
      >
      > Also, you forgot the semicolon.
      >

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

      Yahoo! Groups Links

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




      [Non-text portions of this message have been removed]
    • Noah Sussman
      ... (function (foo) { return foo; })( bar ); -- Noah Sussman Software Historian @noahsussman A foolish consistency is the hobgoblin of little minds      
      Message 2 of 19 , May 3, 2010
      • 0 Attachment
        On Mon, May 3, 2010 at 12:06 PM, Rob Richardson <erobrich@...> wrote:

        >Is there a way to invoke a function with the 'function' keyword present?

        (function (foo) {
        return foo;
        })('bar');


        --
        Noah Sussman
        Software Historian
        @noahsussman

        A foolish consistency is the hobgoblin of little minds
               -- Emerson
      • Rob Richardson
        You re creating it with function then invoking it right away. Is there a similar way to use the keyword function to call a previously defined function?
        Message 3 of 19 , May 3, 2010
        • 0 Attachment
          You're creating it with 'function' then invoking it right away. Is there a similar way to use the keyword 'function' to call a previously defined function? If not, it seems sufficient to note the keyword 'function' will define a function, and it's absence will invoke it. The presence or absence of a space is helpful, but the convention is recent and seems somewhat contrived. Reading code of those who don't subscribe to it will lead to misunderstanding. It seems the presence of the word 'function' is a much better identifier of the purpose of the statement than the presence of a space before the parens.

          Rob





          ________________________________
          From: Noah Sussman <noah@...>
          To: jslint_com@yahoogroups.com
          Sent: Mon, May 3, 2010 9:42:38 AM
          Subject: Re: [jslint] Re: function() -> function ()


          On Mon, May 3, 2010 at 12:06 PM, Rob Richardson <erobrich@yahoo. com> wrote:

          >Is there a way to invoke a function with the 'function' keyword present?

          (function (foo) {
          return foo;
          })('bar');

          --
          Noah Sussman
          Software Historian
          @noahsussman

          A foolish consistency is the hobgoblin of little minds
          -- Emerson





          [Non-text portions of this message have been removed]
        • Cheney, Edward A SSG RES USAR USARC
          Michael, Your example is partially accurate, but consider the following: var f = function () {}; function g () {} if (typeof(f) === typeof(g)) {
          Message 4 of 19 , May 3, 2010
          • 0 Attachment
            Michael,

            Your example is partially accurate, but consider the following:

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

            if (typeof(f) === typeof(g)) {
            alert(typeof(f) + "\n" + f + "\n" + g);
            }

            You are correct in that a name property is supplied to the second of these two cases. But the types are identical and variable f has an anonymous function assigned to it. Since f is a named variable with a function as its value it is a function that is not anonymous. JavaScript is a lambda language of downward inheritance that allows accidental creation of global variables. With regard to complex instances of inheritance where closures are used across the variance namespace scopes you have to be sure where your variables are defined to prevent collisions, especially with consideration for reuse. The first convention forces strict awareness of variable declaration, because the function must be declared before it can be executed. The second convention supplies no such awareness, which is potentially problematic with regards to instantiation and invocation as closure in complex logic prior described. Fortunately, JSLint is smart enough to throw an error when a function is used before it is declared. Since the two conventions are identical in representation I would suggest only using the one that is not open to abuse from flawed and sloppy programming.

            Austin

            ----- Original Message -----
            From: Michael Lorton <mlorton@...>
            Date: Monday, May 3, 2010 2:38
            Subject: Re: [jslint] Re: function() -> function ()
            To: jslint_com@yahoogroups.com


            > Identical? Have you ever heard the expression "not always right,
            > but never in doubt"?
            >
            > What should the following show?
            >
            > var f = function () {
            > };
            >
            > function g() {
            > }
            > alert(f.name == g.name);
            >
            >
            > By any ordinary sense "identical", you'd think it would pop-up
            > "true", but lo, f.name is undefined but g.name is "g".
            >
            > The function pointed to by f is anonymous in the sense that it does
            > not know its own name, although other functions may have a name for
            > it (indeed, it any function does not appear somewhere in the
            > namespace, it's unreferenced and so, can never be called and will
            > be cleaned up by the GC.
            >
            > M.
          Your message has been successfully submitted and would be delivered to recipients shortly.