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

Bad for in variable when using a function argument

Expand Messages
  • Marcel Duran
    Why does JSLint complain when a function argument is used as variable in a for in loop? ex: (function () { var foo = function (bar) { var o = {}; for (bar in
    Message 1 of 2 , Mar 25, 2010
    • 0 Attachment
      Why does JSLint complain when a function argument is used as variable in a for in loop?

      ex:
      (function () {
      var foo = function (bar) {
      var o = {};
      for (bar in o) {
      if (o.hasOwnProperty(bar)) {
      console.log(o[bar]);
      }
      }
      };
      foo('foobar');
      }());

      error:
      Problem at line 4 character 14: Bad for in variable 'bar'.

      This problem came across while code reviewing a piece of code today, I just fixed that by declaring a proper variable inside that function for this specific for in loop task. Just wondering why.
    • Douglas Crockford
      It wants you to be using a local var. Using a parameter looks like a mistake.
      Message 2 of 2 , Mar 25, 2010
      • 0 Attachment
        It wants you to be using a local var. Using a parameter looks like a
        mistake.

        On 3/25/2010 4:49 PM, Marcel Duran wrote:
        > (function () {
        > var foo = function (bar) {
        > var o = {};
        > for (bar in o) {
        > if (o.hasOwnProperty(bar)) {
        > console.log(o[bar]);
        > }
        > }
        > };
        > foo('foobar');
        > }());
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.