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

1728Re: Circular Function Definitions

Expand Messages
  • Jordan
    Jan 5, 2011
    • 0 Attachment
      I think the confusion here is that you shouldn't be using the "circular function" pattern you are using. In addition, you are using function declarations rather than assigning a function statement to a variable.

      What you want to do, it seems, is immediately run requestCommentary after a matchIsStillRunning response comes back from the server.

      What I can think of off the top of my head is:

      var onCommentaryReceived = function (commentary, matchIsStillRunning) {
      // write live commentary: ...
      if (matchIsStillRunning) {
      sendToServer('Berlin:Munich', onCommentaryReceived);
      }
      },
      requestCommentary = function () {
      return onCommentaryReceived(null, true);
      };

      Alternatively, and with less refactoring:
      var onCommentaryReceived, requestCommentary;
      onCommentaryReceived = function (commentary, matchIsStillRunning) {
      // write live commentary: ...
      if (matchIsStillRunning) {
      requestCommentary();
      }
      };
      requestCommentary = function () {
      sendToServer('Berlin:Munich', onCommentaryReceived);
      };

      --- In jslint_com@yahoogroups.com, "Felix E. Klee" <felix.klee@...> wrote:
      >
      > On Tue, Jan 4, 2011 at 7:35 PM, Erik Eckhardt <erik@...>
      > wrote:
      > > no extra Boolean parameter is needed to indicate whether to call a
      > > hard-coded function name.
      >
      > There may be a misunderstanding.
      >
      > Thus, for your pleasure, below a more real-life example. Asides from
      > being more verbose, the only difference to my original example is the
      > addition of the parameter "liveCommentary". Together with the flag
      > "matchIsStillRunning" (formerly: "moreToDo") it forms the data returned
      > from the server.
      >
      > var sendToServer; // defined elsewhere
      >
      > function onCommentaryReceived(commentary, matchIsStillRunning) {
      > // write live commentary: ...
      > if (matchIsStillRunning) {
      > requestCommentary();
      > }
      > }
      >
      > function requestCommentary() {
      > sendToServer('Berlin:Munich', onCommentaryReceived);
      > }
      >
      > Naturally, one could rewrite this:
      >
      > var sendToServer; // defined elsewhere
      >
      > function requestCommentary() {
      > sendToServer('Berlin:Munich',
      > function (commentary, matchIsStillRunning) {
      > // write live commentary: ...
      > if (matchIsStillRunning) {
      > requestCommentary();
      > }
      > });
      > }
      >
      > The second form avoids the circular function definition, but is it more
      > readable?
      >
    • Show all 17 messages in this topic