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

1712RE: [jslint] Circular Function Definitions

Expand Messages
  • Rob Richardson
    Dec 31, 2010
    • 0 Attachment
      Felix,

      The primary concern is you're trying to use a function earlier in your code
      file than when it is defined. If you put var f = function () {... before
      function callback(... then JSLint would likely be fine. I understand that
      given the business rules around the code in question that callback() (and
      thus f()) would never be used before it was defined, but JSLint doesn't know
      that.

      The secondary concern is that you're passing in a flag that says whether you
      should use a follow-up function. Perhaps you refactored f() out of
      callback() for readability, perhaps f() is used in more than one place,
      perhaps you never considered putting the contents of f inside callback.
      Alas, the "moreToDo is true means run f()" is a bit confusing. Passing the
      function to run into callback() instead of a flag makes callback() more
      generic, and makes this point a bit more clear, and that was the focus of
      Erik's thoughts.

      Rob


      -----Original Message-----
      From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
      Behalf Of Felix E. Klee
      Sent: Friday, December 31, 2010 1:12 PM
      To: jslint_com@yahoogroups.com
      Subject: Re: [jslint] Circular Function Definitions

      On Fri, Dec 31, 2010 at 7:21 PM, Erik Eckhardt <erik@...> wrote:
      > function callback(morefn) {
      > // do something ...
      > if (typeof(morefn) === 'function') {
      > moref();
      > }
      > }
      >
      > f = function () {
      > sendToServer('xyz', callback ? f : null);
      > };

      Are you sure this makes sense? The second last line will always evaluate to:

      sendToServer('xyz', f);

      And I don't see how this solves the issue I raised:

      var f;

      function callback(moreToDo) {
      // ...
      }

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

      That's inconsistent, and thus confusing. Of course one could write:

      var callback, f;

      callback = function (moreToDo) {
      // ...
      };

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

      Now things are consistent. But is that the way to go?
    • Show all 17 messages in this topic