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

552[jslint] Re: Undefined variable

Expand Messages
  • sandyhead25
    May 29, 2009
      --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@...> wrote:
      > Hasn't this all gotten a little far from the original problem?
      > The way I understood it, it had to do with "namespaces". If you have two javascript files that both declare some functionality in the same namespace, and you're not sure if you'll be using one file, the other file, or both files (or you are sure but all three cases exist in various parts of the application), then you do need "special tricks" (i.e. not just creating the namespace and putting stuff in it) to avoid the contents of the second file removing everything declared by the first.
      > The simple trick is to start both files like this:
      > var Namespace = Namespace || {};
      > No need to check if Namespace has already been declared, just use the rather powerful 'or' operator.
      > /Jakob
      > ----- Original Message -----
      > From: sandyhead25
      > [mailto:austin.cheney@...]
      > To: jslint_com@yahoogroups.com
      > Sent: Fri,
      > 29 May 2009 01:23:48 +0200
      > Subject: [jslint] Re: Undefined variable
      > > The most correct answer is not test to see if a variable is defined. You
      > > should never have to make such a test unless you are receiving code from
      > > other parties for execution, which is a very bad idea for countless reasons.
      > > If you are writing your own code you should know what variables you are
      > > defining even if the application is very large with complex scoping and
      > > closures.
      > >
      > > The way to ensure this is never a problem is to define your variables first
      > > according to the scope you desire. If that is global then the first thing
      > > in your code should be:
      > >
      > > var a, b, c, d, other_var_name;
      > >
      > > Notice that no value is assigned to those variables. They are merely
      > > declared. Now, you can assign those variables at any time in your
      > > application. If you never assign some of those variables JSLint will tell
      > > you in bold "Unused".
      > >
      > > The only time I have seen this inaccurately reported is in regard to
      > > immediate invocation of a function declared as a variable the same time it
      > > is being declared. Example:
      > >
      > > var a, b,
      > > c = (function () {
      > > do or report something automatically
      > > }());
      > >
      > > In that example JSLint would report var c as unused, which is not accurate.
      > > I do understand why JSLint reports in that way, because the function is
      > > buried within a declaration and there is no evidence of its existence
      > > outside the declaration, which is exactly what it is testing for to
      > > determine "Unused".
      > >
      > > Any rate declare all your variable names first then you don't have to test
      > > to see if something is declared. Instead you only have to ensure it is
      > > used, and if not then remove it.
      > >
      > >

      In that case it is even more simple. If two named values exist within the same scope the only that survives is the one that is declared last. If it is a matter of functions competing for the same scope from different JS files the only one that survives is the one that is in the file sourced last into the HTML.
    • Show all 25 messages in this topic