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

1855Don't make functions within a loop -- what is the preferered pattern?

Expand Messages
  • saj14saj
    Feb 1, 2011
      I have noticed recently that jslint is now giving a problem warning when
      creating functions in a loop.

      Below is an example use case, I am creating a rows in a table and
      linking events to each row (using the Dojo toolkit, but the Dojo'isms in
      the code should be self-explanatory.

      I use the closure to preserve the index value to make the event
      processing easier.

      What pattern is recommended, if jslint considers this to be a
      problematic practice?

      Thanks.

      for (r = 0; r < this.data.length; r += 1) {

      console.log('r', r);

      tr = dojo.create('tr', {
      'class': r === this.selectedIndex ? 'selected' : '',

      'onclick': (function (index) {
      return function () {
      that._onRowClick(index);
      };
      }(r))

      }, this.tbodyNode, 'last');

      ... etc....
      }





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