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

551Re: Undefined variable

Expand Messages
  • sandyhead25
    May 29, 2009
    • 0 Attachment
      > 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.

      > Actually, the ECMAScript specs say that an implementation will
      > see an identifier as a local variable if it is declared *anywhere*
      > in the current scope - not just at the beginning. Look here:
      > var foo = "hey";
      > function blarg () {
      > console.log(foo); // calls log() with an *undefined* value...
      > if (false) {
      > var foo = "baz"; // ...because it's declared as local here
      > }
      > console.log(foo); // still undefined
      > foo = "qux";
      > console.log(foo); // ah, now it's declared *and* defined
      > }
      > This function has a local variable named "foo". It doesn't matter if
      > the contents of the if-branch are ever reached. What JSLint is
      > complaining about in this case is that we used foo _before_ it was
      > declared. This is perfectly legal in JavaScript, but not considered
      > good style. So, what you want to do in this case is:
      > var foo = "hey";
      > function blarg () {
      > var foo;
      > somefunc(foo); // still undefined, but no warning
      > if (false) {
      > foo = "baz";
      > }
      > // etc
      > }

      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. I could have been more specific with this.

      > > 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.
      > 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. By logically reachable I only mean that there are not deliberate conditions, such as an 'if' statement, in place preventing the function, or its container, from being interpreted.

      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
    • Show all 25 messages in this topic