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

561Re: Undefined variable

Expand Messages
  • sandyhead25
    May 31, 2009
      --- In jslint_com@yahoogroups.com, "crlender" <crlender@...> wrote:
      >
      > --- In jslint_com@yahoogroups.com, "sandyhead25" <austin.cheney@> wrote:
      > >
      > > > You mean declared, not defined (big difference).
      > >
      > > I meant exactly what I said. He should test if a variable has been
      > > declared and not whether it has been defined.
      >
      > 1) You said "defined", not "declared"
      > 2) How do you propose to test if a variable has been declared?
      > (hint: you don't; you always know if it's been declared or not).
      >
      > > Scope is local to the object containing the variable declaration,
      > > as a result the scope of a variable is chosen based upon which
      > > object in it is declared in an object hierarchy. This is an
      > > important distinction apart from considering variables as only
      > > local to their object of instantiation for the sake of closures.
      >
      > There is only global and function scope in JavaScript. Saying "scope
      > is local to an object" is incorrect.
      >
      > > > > var a, b,
      > > > > c = (function () {
      > > > > do or report something automatically
      > > > > }());
      > > > >
      > > > > In that example JSLint would report var c as unused, which is not
      > > > > accurate.
      > > >
      > > > Unless you use c inside the (function)(), or further down in the
      > > > current scope, it definitely will be unused. What's the problem?
      > >
      > > You are wrong. The practice is called immediate invocation, and
      > > JSLint has an option to enforce wrapping such practices in
      > > parentheses. Any function that is logically reachable and properly
      > > closed with an ending curly brace and immediately followed by a
      > > parenthesis pair is executed immediately without regard for where
      > > that function exists.
      >
      > That doesn't have anything to do with what I wrote. If you declare
      > a variable and then don't use it, you get a warning.
      >
      > > Don't just take my word for it try it yourself. An example of this
      > > is the 'it' function in my markupmin application available at
      > > http://mailmarkup.org/prettydiff/prettydiff.html
      >
      > I don't have time to analyze all of that, but it looks like you're
      > declaring a local variable called "it", which you don't use in its
      > scope, leading to the (correct) JSLint warning. What's the point of
      > this variable? And what's the point of that anonymous function there?
      > You don't use it to introduce a new scope, and it doesn't return
      > anything (meaning |it| will be assigned a value of |undefined|).
      > None of this makes sense:
      >
      > function () {
      > var ...,
      > it = (function() {
      > // some loop that doesn't return a value
      > })();
      > // note: |it| is only used once in this scope
      > }
      >
      > You should drop both |it| and the anon function:
      >
      > function () {
      > var ...;
      > // some loop that doesn't return a value
      > }
      >
      >
      > - Conrad
      >

      Your example is invalid. You ended your function with })(); but it should have been }()); if you wishes to assert a function of immediate invocation. Try to execute this code and let me know what happens:

      "use strict";
      var a = 1, b = 2, c = 3,
      d = (function () {
      var z = (function () {
      var e = a + 3;
      alert(e + b + c);
      }());
      }());
      alert(a + b + c);

      According to your claim function d and z are unused and so only one alert will fire with a value of 6. JSLint will tell you function z is not used. However, z is used. I just tried this myself.

      I figured out the problem with the JSLint reporting. If a function of immediate invocation is contained by a function it will show as unused even if the parent function exists in the global space and is itself a function of immediately invocation, which is not accurate.
    • Show all 25 messages in this topic