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

Question about ===

Expand Messages
  • 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 1 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 2 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 3 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 4 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 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.

              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 6 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.