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

1720Re: [jslint] Circular Function Definitions

Expand Messages
  • Erik Eckhardt
    Jan 4, 2011
    • 0 Attachment
      If your example isn't the full story, please post an example of the full
      story!

      One thing that's wrong with the circularity is the cross-dependence as
      JSLint is showing you. It's not a huge deal, but an annoying one, perhaps.

      In any case I thought you might find it useful to use some of the techniques
      I mentioned, where if a function determines that an additional step is
      needed, it manages it within the function rather than making the callback
      function have a hard coded reference back. The scheme you're using now seems
      to make sense but as your application grows you may find that it becomes
      unworkable. For example, what if you eventually have two functions that may
      or may not need to be called after an xhr? Are you going to pass two
      booleans? Or God forbid, a code indicating which functions to run? If from
      the start you simply passed around single callback functions (which
      themselves could be a string of functions) this problem would be solved.

      function myxhr(url, params, callback) {
      ___//do stuff;
      doXhr(function() {myxhrreturn(xmlhttp, callback):});
      }

      function myxhrreturn(xmlhttp, callback) {
      ___//process xmlhttp object into a result
      ___callback(result);
      }

      function getsomething() {
      ___myxhr('http://example.com', 'a=1', function(result) {
      ______if (result.blah === 'gorp') {
      _________dosomething;
      ______} else {
      _________dosomethingelse();
      ______}
      ___}
      }

      Now your getsomething function controls everything and all your xmlhttp
      requests can be handled the same way without having to make a hardcoded
      call. If you want the logic in the myxhrreturn function, you can put it
      there, but it's not required. Here's another way to do it:

      function getsomething() {
      var cb;
      if (somecondition) {
      ______cb = function(result) {dosomething(result)};
      ___} else {
      ______cb = function(result) {dosomethingelse(result);};
      ___}
      ___myxhr('http://example.com', 'a=1', cb}
      }

      See how versatile this is? You only need a single callback, no extra Boolean
      parameter is needed to indicate whether to call a hard-coded function name.
      Any time you want more stuff to happen, you can load it into the passed
      callback function somehow.

      Erik

      On Tue, Jan 4, 2011 at 1:47 AM, Felix E. Klee <felix.klee@...> wrote:

      >
      >
      > On Tue, Jan 4, 2011 at 7:53 AM, Erik Eckhardt <erik@...<erik%40eckhardts.com>>
      > wrote:
      > > I thought you were using a Boolean to indicate whether to make the
      > > return call to f.
      >
      > No, that was correct. In the example code, I *was* using a boolean. Only
      > in the real code, things are more complex:
      >
      > Based on what the XHR returns some calculations are performed, and - if
      > needed - another XHR is made.
      >
      >
      > > The point I was trying to get across is that you don't need the
      > > circularity.
      >
      > What's bad about the circularity?
      >
      >
      > > xhr(params, function() {callback(); f();}); // circular call back to f
      >
      > That would again be an infinite loop. The body of the anonymous function
      > would need to be at least slightly more complex. I don't think this is
      > increases readability.
      >
      >


      [Non-text portions of this message have been removed]
    • Show all 17 messages in this topic