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

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

Expand Messages
  • Michael Lorton
    May 2, 2010
    • 0 Attachment
      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@...>
      To: jslint_com@yahoogroups.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@...>
      Date: Saturday, May 1, 2010 23:53
      Subject: [jslint] Re: function() -> function ()
      To: jslint_com@yahoogroups.com


      > --- In jslint_com@yahoogroups.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]
    • Show all 19 messages in this topic