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

Undetected variable

Expand Messages
  • Fred Lorrain
    In this example: var getObjLength = function(obj) { var objLength = 0, key = ; for (key in obj) { objLength++; } return objLength; }; The key variable is not
    Message 1 of 4 , Apr 15, 2009
    • 0 Attachment
      In this example:

      var getObjLength = function(obj) {
      var objLength = 0,
      key = '';

      for (key in obj) {
      objLength++;
      }
      return objLength;
      };

      The key variable is not detected.

      Here the report of JSLint:

      Global getObjLength
      14 "getObjLength"(obj)Variable objLengthUnused key



      [Non-text portions of this message have been removed]
    • santini.alberto
      ... It is correct because there is an error in the snippet: the body of a for in should be wrapped in an if statement to filter unwanted properties from the
      Message 2 of 4 , Apr 15, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@...> wrote:
        >
        > The key variable is not detected.

        It is correct because there is an error in the snippet: the body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.

        ...
        for (key in obj) {
        if (obj.hasOwnProperty(key)) {
        objLength++;
        }
        }
        ...

        Regards,
        Alberto
      • Fred Lorrain
        Ok, thank you very much. I should have read your JavaScript recommendations before asking. What a shame .hasOwnProperty() is not documented in many JavaScript
        Message 3 of 4 , Apr 16, 2009
        • 0 Attachment
          Ok, thank you very much.
          I should have read your JavaScript recommendations before asking.

          What a shame .hasOwnProperty() is not documented in many JavaScript references

          --- In jslint_com@yahoogroups.com, "santini.alberto" <albertosantini@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@> wrote:
          > >
          > > The key variable is not detected.
          >
          > It is correct because there is an error in the snippet: the body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.
          >
          > ...
          > for (key in obj) {
          > if (obj.hasOwnProperty(key)) {
          > objLength++;
          > }
          > }
          > ...
          >
          > Regards,
          > Alberto
          >
        • Douglas Crockford
          ... Thanks. This has been fixed.
          Message 4 of 4 , Apr 17, 2009
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Fred Lorrain" <yahoo@...> wrote:
            > for (key in obj) {

            > The key variable is not detected.

            Thanks. This has been fixed.
          Your message has been successfully submitted and would be delivered to recipients shortly.