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

Variable x was used before it was declared.

Expand Messages
  • Toby Maxwell-Lyte
    Hi, When JSLinting there are several scenarios where we get the Variable {a} was used before it was declared. message. This usually occurs when we define
    Message 1 of 9 , Oct 1, 2009
    • 0 Attachment
      Hi,

      When JSLinting there are several scenarios where we get the "Variable
      {a} was used before it was declared. " message.

      This usually occurs when we define static constants before the constructor. E.g.

      MyClass.CONSTANT = 23;

      function MyClass () {
      }

      MyClass.prototype.myMethod = function () {
      }

      What is the rational behind this check?
      Which of the jslint options disables it?

      I was looking at the documentation page specfically at the undef option.
      undef : true if variables must be declared before used. (more)
      I think that there is a bug with the
      http://www.jslint.com/lint.html#undefined anchor that's linked to from
      the (more) link.

      Many thanks,
      Toby
    • Douglas Crockford
      ... Declare names before they are used. Write it this way: function MyClass () { } MyClass.CONSTANT = 23; MyClass.prototype.myMethod = function () { };
      Message 2 of 9 , Oct 1, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Toby Maxwell-Lyte <toby.maxwelllyte@...> wrote:
        > When JSLinting there are several scenarios where we get the "Variable
        > {a} was used before it was declared. " message.
        >
        > This usually occurs when we define static constants before the constructor. E.g.
        >
        > MyClass.CONSTANT = 23;
        >
        > function MyClass () {
        > }
        >
        > MyClass.prototype.myMethod = function () {
        > }
        >
        > What is the rational behind this check?

        Declare names before they are used. Write it this way:

        function MyClass () {
        }

        MyClass.CONSTANT = 23;

        MyClass.prototype.myMethod = function () {
        };
      • Toby Maxwell-Lyte
        ... Thanks, it just seems logical to have constants at the top of the file. What is the motivation behind this rule?
        Message 3 of 9 , Oct 1, 2009
        • 0 Attachment
          2009/10/1 Douglas Crockford <douglas@...>:
          > --- In jslint_com@yahoogroups.com, Toby Maxwell-Lyte <toby.maxwelllyte@...> wrote:
          >> When JSLinting there are several scenarios where we get the "Variable
          >> {a} was used before it was declared. " message.
          >>
          >> This usually occurs when we define static constants before the constructor. E.g.
          >>
          >> MyClass.CONSTANT = 23;
          >>
          >> function MyClass () {
          >> }
          >>
          >> MyClass.prototype.myMethod = function () {
          >> }
          >>
          >> What is the rational behind this check?
          >
          > Declare names before they are used. Write it this way:
          >
          >    function MyClass () {
          >    }
          >
          >    MyClass.CONSTANT = 23;
          >
          >    MyClass.prototype.myMethod = function () {
          >    };
          >

          Thanks, it just seems logical to have constants at the top of the
          file. What is the motivation behind this rule?
        • Nagy Endre
          I have a string - base_url - with an url inside. I want to test if this is the current url on my site. If I use window.location === base_url jslint are ok,
          Message 4 of 9 , Oct 2, 2009
          • 0 Attachment
            I have a string - base_url - with an url inside. I want to test if this is the current url on my site.
            If I use
            window.location === base_url
            jslint are ok, but the result is false because the type are not the same I think.
            for
            window.location == base_url
            I get true, but this is not a good practice. What is the good practice in this case?

            Thanks





            [Non-text portions of this message have been removed]
          • pauanyu
            ... JavaScript doesn t have constants (yet). Having values assigned before they are declared can sometimes require you to jump back and forth through the code
            Message 5 of 9 , Oct 2, 2009
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, Toby Maxwell-Lyte <toby.maxwelllyte@...> wrote:
              >
              > Thanks, it just seems logical to have constants at the top of the
              > file. What is the motivation behind this rule?
              >

              JavaScript doesn't have constants (yet).

              Having values assigned before they are declared can sometimes require you to jump back and forth through the code to understand it's structure.

              By always declaring your variables (and consequently, objects) before assigning values to them, it can make the code structure clearer.
            • pauanyu
              ... location is an object, use location.href instead: if (location.href === base_url) { } If that doesn t solve the problem... we will need more information,
              Message 6 of 9 , Oct 2, 2009
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, Nagy Endre <forewer2000@...> wrote:
                >
                > I have a string - base_url - with an url inside. I want to test if this is the current url on my site.
                > If I use
                > window.location === base_url
                > jslint are ok, but the result is false because the type are not the same I think.
                > for
                > window.location == base_url
                > I get true, but this is not a good practice. What is the good practice in this case?
                >
                > Thanks
                >

                location is an object, use location.href instead:

                if (location.href === base_url) {
                }

                If that doesn't solve the problem... we will need more information, like the actual code you are using.
              • samckayak
                ... You could try: window.location.toString() === base_url.toString();
                Message 7 of 9 , Oct 2, 2009
                • 0 Attachment
                  --- In jslint_com@yahoogroups.com, Nagy Endre <forewer2000@...> wrote:
                  >
                  > I have a string - base_url - with an url inside. I want to test if this is the current url on my site.
                  > If I use
                  > window.location === base_url
                  > jslint are ok, but the result is false because the type are not the same I think.
                  > for
                  > window.location == base_url
                  > I get true, but this is not a good practice. What is the good practice in this case?

                  You could try:

                  window.location.toString() === base_url.toString();
                • Douglas Crockford
                  ... They aren t constants. They are mutable properties of a function. ... Variables should be declared before they are used. The function statement is a
                  Message 8 of 9 , Oct 2, 2009
                  • 0 Attachment
                    --- In jslint_com@yahoogroups.com, Toby Maxwell-Lyte <toby.maxwelllyte@...> wrote:

                    > Thanks, it just seems logical to have constants at the top of the
                    > file.

                    They aren't constants. They are mutable properties of a function.

                    > What is the motivation behind this rule?

                    Variables should be declared before they are used.
                    The function statement is a variable declaration.
                  • Toby Maxwell-Lyte
                    ... Thanks for the explanations gents.
                    Message 9 of 9 , Oct 2, 2009
                    • 0 Attachment
                      2009/10/2 Douglas Crockford <douglas@...>:
                      > --- In jslint_com@yahoogroups.com, Toby Maxwell-Lyte <toby.maxwelllyte@...> wrote:
                      >
                      >> Thanks, it just seems logical to have constants at the top of the
                      >> file.
                      >
                      > They aren't constants. They are mutable properties of a function.
                      >
                      >> What is the motivation behind this rule?
                      >
                      > Variables should be declared before they are used.
                      > The function statement is a variable declaration.
                      >

                      Thanks for the explanations gents.
                    Your message has been successfully submitted and would be delivered to recipients shortly.