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

562Re: Undefined variable

Expand Messages
  • crlender
    May 31, 2009
      --- In jslint_com@yahoogroups.com, "sandyhead25" <austin.cheney@...> wrote:
      >
      > --- In jslint_com@yahoogroups.com, "crlender" <crlender@> wrote:
      ...
      > > [..] 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.

      sandyhead, it's not invalid. The parens around (function) aren't
      required at all, they are merely a convention to indicate to the
      reader that this is not an ordinary function expression. You could
      just as well write

      var x = function () { /*code*/ }();

      JSLint has an option to enforce this convention (a Good Thing, in my
      opinion), but has recently changed where the parens should be placed.
      I do not agree with this change (and said so in this group), but
      there's not much I can do about it. There are a lot more people who
      place the parens the way I do, just take a look at the major
      libraries like Dojo, YUI, JQuery, or others - none of them use the
      new JSLint-approved style. I think it's too restrictive to demand one
      of the two styles, when the intention is perfectly clear with both.

      > 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);

      Ah. And now try the same thing, except do an 'alert(d)' in the last
      line. See what I mean? Both (function)s are executed, of course, but
      that's not the point. The point is that the *variables* d and z are
      never used.

      > According to your claim function d and z are unused

      Neither d or z are functions. That's the problem.


      - Conrad
    • Show all 25 messages in this topic