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

Re: Undefined variable

Expand Messages
  • Douglas Crockford
    ... Variable z is in fact unused. The proof of that is that you can delete var z = from that program and the result will be exactly the same.
    Message 1 of 25 , May 31, 2009
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "sandyhead25" <austin.cheney@...> wrote:
      > "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.

      Variable z is in fact unused. The proof of that is that you can
      delete "var z = " from that program and the result will be exactly
      the same.
    • sandyhead25
      ... Is execution not an indication of use? I would think if something were properly executed it is used code by the interpreter even if that usage, or any
      Message 2 of 25 , Jun 1, 2009
      • 0 Attachment
        > 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.

        Is execution not an indication of use? I would think if something were properly executed it is used code by the interpreter even if that usage, or any other usage, does not result in any restatement for execution in remaining logic.

        Furthermore when function d from the example is alerted it returns undefined only because it returns nothing after it executes. This is equivalent to declaring a variable and not defining it. If a return and a quoted string are placed between the end of d and z then d alerts as a value. This is no indication that d is unused.
      • sandyhead25
        ... When I execute the program I am presented with a first alert displaying 9 and then a second alert displaying 6 . If I remove z from the program the
        Message 3 of 25 , Jun 1, 2009
        • 0 Attachment
          > Variable z is in fact unused. The proof of that is that you can
          > delete "var z = " from that program and the result will be exactly
          > the same.
          >

          When I execute the program I am presented with a first alert displaying "9" and then a second alert displaying "6". If I remove z from the program the first alert will no longer be present, which is an alteration to the resultant execution even if all other code cannot see or feel that alteration.

          This lack of connection implies that functions of immediate invocation contained as instantiation declarations within other objects have a special unique context until such functions are any way restated or reused outside their declaration. That special unique context would be largely meaningless if not for objects that contain closures and several immediate invocation functions as declarations using those closures after the closure variables are declared.
        Your message has been successfully submitted and would be delivered to recipients shortly.