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

Re: Undefined variable

Expand Messages
  • sandyhead25
    ... 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
    Message 1 of 25 , May 31, 2009
    • 0 Attachment
      --- 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.
    • crlender
      ... 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
      Message 2 of 25 , May 31, 2009
      • 0 Attachment
        --- 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
      • 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 3 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 4 of 25 , Jun 1 5:09 AM
          • 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 5 of 25 , Jun 1 5:28 AM
            • 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.