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

Re: [jslint] Re: has not been fully defined yet

Expand Messages
  • John Hawkinson
    ... This is a bit more troublesome for event handlers. Leaving the browser world for a moment, I encountered this morning: (function() { var pdfdone =
    Message 1 of 24 , Mar 9 5:26 AM
    • 0 Attachment
      > No, it isn't. Declare the variable before giving it a value that
      > depends on the same variable. It is easy.

      This is a bit more troublesome for event handlers.

      Leaving the browser world for a moment, I encountered this morning:

      (function() {
      var pdfdone = doc.eventListeners.add("afterExport", function(e) {
      if (e.format==="whatever") {
      pdfdone.remove();
      }
      // ...
      });
      }());

      Which hits:

      Problem at line 4 character 7: 'pdfdone' has not been fully defined yet.

      Am I required to give up the anonymous function paradigm? It's very
      nice to not have to bind a name to this function. But in this API, the
      only way I have to remove the event handler is to invoke a property
      returned from adding the event handler.

      Is there anything short of:

      (function() {

      var pdfdone;

      function handler(e) {
      if (e.format==="whatever") {
      pdfdone.remove();
      }
      // ...
      }

      pdfdone = doc.eventListeners.add("afterExport", handler);
      }());

      That makes me somewhat unhappy. Could we have a
      /*jslint*/ directive so I can mark that function scope as being safe
      for this?


      (Philosophically, I am a bit puzzled why JSLint doesnt
      have a /*LINTED*/ equivalent for single lines. Would it
      really encourage such bad programming?)

      --jhawk@...
      John Hawkinson
    • Kent Davidson
      I gotta chime here and agree with Douglas. However, I think jslint 2011-03-07 is broken: var dude = 20 * dude; fails to throw an error at all: FireFox 3.6:
      Message 2 of 24 , Mar 9 11:12 AM
      • 0 Attachment
        I gotta chime here and agree with Douglas. However, I think jslint 2011-03-07 is broken:

        var dude = 20 * dude;

        fails to throw an error at all:

        FireFox 3.6: /*jslint browser: true, white: true, onevar: true, undef: true, newcap: true, nomen: true, regexp: true, plusplus: true, bitwise: true, maxerr: 50, indent: 4 */

        But I agree in principle that you shouldn't use variables in their assignment upon declaration.

        I tend to think of this as if I were implementing the actual JavaScript parser:

        var poll = setInterval(function () {
        if (/loaded|complete/i.test(document.readyState)) {
        clearInterval(poll);
        return true;
        }
        }, 10);

        Here, you are using the value of "poll" before the value has been completely defined and assigned. I assume most browsers do lazy evaluation of variables, which is why it works, but I guess what jslint does is eliminate this potentially confusing issue, and my guess is that it's difficult to determine the difference between the above, and:

        var poll = 20 * poll;

        Which is an error, unless you want it to be "undefined".

        The solution is simply to add a semicolon and repeat the variable.

        var poll;
        poll = setInterval(function () {
        if (/loaded|complete/i.test(document.readyState)) {
        clearInterval(poll);
        return true;
        }
        }, 10);

        However, running this code (below) through jslint (2011-03-07) produced no errors, even with "The Good Parts" turned on:

        var poll = setInterval(function () {
        if (/loaded|complete/i.test(document.readyState)) {
        clearInterval(poll);
        return true;
        }
        }, 10);

        As I mentioned, I think something's broken.

        So, John, you can fix your code by simply doing this:

        <snip>
        (function() {
        var pdfdone;
        pdfdone = doc.eventListeners.add("afterExport", function(e) {
        if (e.format==="whatever") {
        pdfdone.remove();
        }
        })
        <snip>

        Cheers,

        -Kent.

        On Mar 9, 2011, at 8:26 AM, John Hawkinson wrote:

        >
        > > No, it isn't. Declare the variable before giving it a value that
        > > depends on the same variable. It is easy.
        >
        > This is a bit more troublesome for event handlers.
        >
        > Leaving the browser world for a moment, I encountered this morning:
        >
        > (function() {
        > var pdfdone = doc.eventListeners.add("afterExport", function(e) {
        > if (e.format==="whatever") {
        > pdfdone.remove();
        > }
        > // ...
        > });
        > }());
        >
        > Which hits:
        >
        > Problem at line 4 character 7: 'pdfdone' has not been fully defined yet.
        >
        > Am I required to give up the anonymous function paradigm? It's very
        > nice to not have to bind a name to this function. But in this API, the
        > only way I have to remove the event handler is to invoke a property
        > returned from adding the event handler.
        >
        > Is there anything short of:
        >
        > (function() {
        >
        > var pdfdone;
        >
        > function handler(e) {
        > if (e.format==="whatever") {
        > pdfdone.remove();
        > }
        > // ...
        > }
        >
        > pdfdone = doc.eventListeners.add("afterExport", handler);
        > }());
        >
        > That makes me somewhat unhappy. Could we have a
        > /*jslint*/ directive so I can mark that function scope as being safe
        > for this?
        >
        > (Philosophically, I am a bit puzzled why JSLint doesnt
        > have a /*LINTED*/ equivalent for single lines. Would it
        > really encourage such bad programming?)
        >
        > --jhawk@...
        > John Hawkinson
        >



        [Non-text portions of this message have been removed]
      • Douglas Crockford
        ... Global variables are weird and obey different rules. JSLint already reports global variables. A statement like var GLOBAL = GLOBAL || {}; actually makes
        Message 3 of 24 , Mar 9 11:19 AM
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, Kent Davidson <kent@...> wrote:
          >
          > I gotta chime here and agree with Douglas. However, I think jslint 2011-03-07 is broken:
          >
          > var dude = 20 * dude;
          >
          > fails to throw an error at all


          Global variables are weird and obey different rules.
          JSLint already reports global variables.

          A statement like

          var GLOBAL = GLOBAL || {};

          actually makes sense at the global level.
        • Kent Davidson
          Ah. So the canonical error would be: function foo () { var dude = 20 * dude; } Another reason to avoid globals. Chers, -Kent. ... [Non-text portions of this
          Message 4 of 24 , Mar 9 11:27 AM
          • 0 Attachment
            Ah. So the canonical error would be:

            function foo () {
            var dude = 20 * dude;
            }

            Another reason to avoid globals.

            Chers,

            -Kent.

            On Mar 9, 2011, at 2:19 PM, Douglas Crockford wrote:

            >
            > --- In jslint_com@yahoogroups.com, Kent Davidson <kent@...> wrote:
            > >
            > > I gotta chime here and agree with Douglas. However, I think jslint 2011-03-07 is broken:
            > >
            > > var dude = 20 * dude;
            > >
            > > fails to throw an error at all
            >
            > Global variables are weird and obey different rules.
            > JSLint already reports global variables.
            >
            > A statement like
            >
            > var GLOBAL = GLOBAL || {};
            >
            > actually makes sense at the global level.
            >
            >



            [Non-text portions of this message have been removed]
          • John Hawkinson
            Kent Davidson wrote on Wed, 9 Mar 2011 ... Wait, is this not bad advice? You are rewriting the code from a form JSLint detects as bad
            Message 5 of 24 , Mar 9 11:46 AM
            • 0 Attachment
              Kent Davidson <kent@...> wrote on Wed, 9 Mar 2011
              at 14:12:41 -0500 in <46D9D4DF-08B8-4820-AE1F-179966621208@...>:

              > Which is an error, unless you want it to be "undefined".
              > The solution is simply to add a semicolon and repeat the variable.

              Wait, is this not bad advice?

              You are rewriting the code from a form JSLint detects as bad to a form
              that is equally bad, but escapes JSLint's detection because it is too
              hard to detect. In essence, you are encouraging programmers to defeat
              the limited forms of checking we have with a construct that can easily
              evolve later. Especially in Javascript where we lack block scope and
              JSLint encourages us (onevar: true) to declare variables far from
              their point of use.

              > var pdfdone;
              > pdfdone = doc.eventListeners.add("afterExport", function(e) {
              > if (e.format==="whatever") {
              > pdfdone.remove();
              > }

              This seems a classic case of the Law of Unintended Consequences. JSLint
              encouraging us to write code it cannot check to defeat its warning?

              In what's ironic, though, I had actually misinterpretted the problem
              in my actual code, because it actually did separate the declaration
              from point-of-use. But there was a typo in the declaration so the
              variable was implicitly global, and when I built a small test-case to
              understand JSLint's behavior, I inadvertantly fixed the typo.

              --jhawk@...
              John Hawkinson
            • Douglas Crockford
              ... It seems like good advice.
              Message 6 of 24 , Mar 9 12:11 PM
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, John Hawkinson <jhawk@...> wrote:
                >
                > Kent Davidson <kent@...> wrote on Wed, 9 Mar 2011
                > at 14:12:41 -0500 in <46D9D4DF-08B8-4820-AE1F-179966621208@...>:
                >
                > > Which is an error, unless you want it to be "undefined".
                > > The solution is simply to add a semicolon and repeat the variable.
                >
                > Wait, is this not bad advice?

                It seems like good advice.
              • Kent Davidson
                Good advice, bad advice. Matter of opinion. If you think it s bad advice, then ignore that warning. I set up my scripts to run through a local copy of jslint
                Message 7 of 24 , Mar 9 12:40 PM
                • 0 Attachment
                  Good advice, bad advice. Matter of opinion. If you think it's bad advice, then ignore that warning. I set up my scripts to run through a local copy of jslint for testing. Any errors produced are considered blocking for shipping new versions. If I don't like a warning, I code the test to ignore the warning. You should do the same.

                  I agree, Douglas could go and rewrite his parsing engine to discover and detect when anonymous functions declared and assigned to the same var that are used within the function itself are OK, but in all honestly, it's debatable whether it will work correctly or in all cases. I'm sure there's some theoretical proof of closure to determine if using a variable in a function produces an unintended side effect.

                  I'm more of an engineer, than a researcher. I test with 50+ browsers when I ship JS code. So, given that I have to work with the least common denominator, I tend to err on the side of being more conservative.

                  Hence I think the absolute rule that in general, it's safer to simply declare the variable, then use it.

                  Whenever I work in any language, I turn all lint options on their highest levels. Any warning/error/notice sometimes shows an error. I also tend to trust that most lint tools do find errors more often than they cramp developers style.

                  That said, Douglas has his own style and advice regarding JavaScript which he has emblazoned on jslint and his personal site. I have read a lot of it, and I gotta say, he's not a complete buffoon. Pretty much the opposite.

                  If you disagree, I completely respect that. However, given his experience, and the fact that he wrote a lint program in JavaScript, I tend to follow his advice.

                  Not to be a sycophant, but I just have gratitude for the work put into this tool.

                  Cheers,

                  -Kent.

                  On Mar 9, 2011, at 2:46 PM, John Hawkinson wrote:

                  > Kent Davidson <kent@...> wrote on Wed, 9 Mar 2011
                  > at 14:12:41 -0500 in <46D9D4DF-08B8-4820-AE1F-179966621208@...>:
                  >
                  > > Which is an error, unless you want it to be "undefined".
                  > > The solution is simply to add a semicolon and repeat the variable.
                  >
                  > Wait, is this not bad advice?
                  >
                  > You are rewriting the code from a form JSLint detects as bad to a form
                  > that is equally bad, but escapes JSLint's detection because it is too
                  > hard to detect. In essence, you are encouraging programmers to defeat
                  > the limited forms of checking we have with a construct that can easily
                  > evolve later. Especially in Javascript where we lack block scope and
                  > JSLint encourages us (onevar: true) to declare variables far from
                  > their point of use.
                  >
                  > > var pdfdone;
                  > > pdfdone = doc.eventListeners.add("afterExport", function(e) {
                  > > if (e.format==="whatever") {
                  > > pdfdone.remove();
                  > > }
                  >
                  > This seems a classic case of the Law of Unintended Consequences. JSLint
                  > encouraging us to write code it cannot check to defeat its warning?
                  >
                  > In what's ironic, though, I had actually misinterpretted the problem
                  > in my actual code, because it actually did separate the declaration
                  > from point-of-use. But there was a typo in the declaration so the
                  > variable was implicitly global, and when I built a small test-case to
                  > understand JSLint's behavior, I inadvertantly fixed the typo.
                  >
                  > --jhawk@...
                  > John Hawkinson
                  >



                  [Non-text portions of this message have been removed]
                • John Hawkinson
                  Kent Davidson wrote on Wed, 9 Mar 2011 ... Well, help me out here. It seemed like bad advice to me, for the reason I articulated
                  Message 8 of 24 , Mar 9 3:40 PM
                  • 0 Attachment
                    Kent Davidson <kent@...> wrote on Wed, 9 Mar 2011
                    at 15:40:57 -0500 in <3E0C5AD2-D3BD-4B8F-B8BA-DF57B308A8B9@...>:

                    > Good advice, bad advice. Matter of opinion. If you think it's bad
                    > advice, then ignore that warning. I set up my scripts to run through

                    Well, help me out here. It seemed like bad advice to me, for the
                    reason I articulated (doesn't reduce the liklihood of a problem,
                    just makes JSLint stop warning). I'm curious why you disagree [and Douglas]?

                    I'm happy to be convinced that it is good advice, but I'd like to know
                    why.


                    > a local copy of jslint for testing. Any errors produced are
                    > considered blocking for shipping new versions. If I don't like a
                    > warning, I code the test to ignore the warning. You should do the
                    > same.

                    Well, I definitely don't want to do that without understanding why
                    other people think the warning is right. Sometimes I might decide I
                    am right and Douglas/JSLint are wrong. But I want to make some
                    effort to be sure I'm gith before deciding that.

                    > I agree, Douglas could go and rewrite his parsing engine to discover
                    > and detect when anonymous functions declared and assigned to the
                    > same var that are used within the function itself are OK,

                    No, I agree that's way too much work. What I want is to be able to write:

                    var pdfdone = doc.eventListeners.add("afterExport", function(e) {
                    /*jslint toleratenotdefined: true */
                    if (e.format==="whatever") {
                    pdfdone.remove();
                    }
                    }


                    Though in the general case I'd like to be able to write:

                    var pdfdone = doc.eventListeners.add("afterExport", function(e) {
                    if (e.format==="whatever") {
                    pdfdone.remove(); /*jslinted*/
                    }
                    }


                    > Hence I think the absolute rule that in general, it's safer to
                    > simply declare the variable, then use it.

                    Did you understand my point, though? Declaring the variable
                    does not confirm that it has a value before it is referenced.
                    It could well still be "undefined". And that would be equally bad.

                    > If you disagree, I completely respect that. However, given his
                    > experience, and the fact that he wrote a lint program in JavaScript,
                    > I tend to follow his advice.

                    I don't disagree. Quite the reverse. It's why I'm concerned that
                    your solution doesn't fix the real problem, it just makes the warning
                    go away. And there are plenty of cases where that's not safe.

                    Am I making more sense now?

                    --jhawk@...
                    John Hawkinson
                  • Kent Davidson
                    ... Hm. Well. I think so, but you say you want to write it a certain way, yet that way is flagged as an error. So, if you agree with JSLint, then you d change
                    Message 9 of 24 , Mar 10 6:18 AM
                    • 0 Attachment
                      >Am I making more sense now?

                      Hm. Well. I think so, but you say you want to write it a certain way, yet that way is flagged as an error. So, if you agree with JSLint, then you'd change the code.

                      Here's my take.

                      If you can imagine a case when doc.eventListeners.add calls your anonymous function immediately, before returning a value, then it's an error. pdfdone is undefined, and pdfdone.remove() is an error. That's why I think the warning is right. A canonical example would be:

                      (function () {
                      var foo = setInterval(function() { clearInterval(foo); dosomethingelse(); }, 0);
                      }());

                      For the cases when the anonymous function is called immediately, I believe, the JSLint advice is, in general, good advice. Like I said, it's unreasonable and, more likely, futile, for someone to code a lint filter to determine if the function is called right away, or not.

                      Most likely, in your case, "afterExport" is called much much later, in which case, it's really your judgement call. It will work. It will likely work 100% of the time. So you can just ignore the warning. My point previously was that regardless of warnings or not, if a small tweak in my code will eliminate the warning, I err on the side of just making the cosmetic change, even if I know the warning

                      So, if you want to fix the core of the issue, then be safe as you indicated, like so:

                      (function() {
                      var pdfdone = null;
                      pdfdone = doc.eventListeners.add("afterExport", function(e) {
                      if (e.format==="whatever") {
                      if (pdfdone) {
                      pdfdone.remove();
                      }
                      }
                      // ...
                      });
                      }());

                      And, like I said, you can simply agree with the error, but not in this case. Your call.

                      Best,

                      -Kent.

                      On Mar 9, 2011, at 6:40 PM, John Hawkinson wrote:

                      > Kent Davidson <kent@...> wrote on Wed, 9 Mar 2011
                      > at 15:40:57 -0500 in <3E0C5AD2-D3BD-4B8F-B8BA-DF57B308A8B9@...>:
                      >
                      > > Good advice, bad advice. Matter of opinion. If you think it's bad
                      > > advice, then ignore that warning. I set up my scripts to run through
                      >
                      > Well, help me out here. It seemed like bad advice to me, for the
                      > reason I articulated (doesn't reduce the liklihood of a problem,
                      > just makes JSLint stop warning). I'm curious why you disagree [and Douglas]?
                      >
                      > I'm happy to be convinced that it is good advice, but I'd like to know
                      > why.
                      >
                      > > a local copy of jslint for testing. Any errors produced are
                      > > considered blocking for shipping new versions. If I don't like a
                      > > warning, I code the test to ignore the warning. You should do the
                      > > same.
                      >
                      > Well, I definitely don't want to do that without understanding why
                      > other people think the warning is right. Sometimes I might decide I
                      > am right and Douglas/JSLint are wrong. But I want to make some
                      > effort to be sure I'm gith before deciding that.
                      >
                      > > I agree, Douglas could go and rewrite his parsing engine to discover
                      > > and detect when anonymous functions declared and assigned to the
                      > > same var that are used within the function itself are OK,
                      >
                      > No, I agree that's way too much work. What I want is to be able to write:
                      >
                      > var pdfdone = doc.eventListeners.add("afterExport", function(e) {
                      > /*jslint toleratenotdefined: true */
                      > if (e.format==="whatever") {
                      > pdfdone.remove();
                      > }
                      > }
                      >
                      > Though in the general case I'd like to be able to write:
                      >
                      > var pdfdone = doc.eventListeners.add("afterExport", function(e) {
                      > if (e.format==="whatever") {
                      > pdfdone.remove(); /*jslinted*/
                      > }
                      > }
                      >
                      > > Hence I think the absolute rule that in general, it's safer to
                      > > simply declare the variable, then use it.
                      >
                      > Did you understand my point, though? Declaring the variable
                      > does not confirm that it has a value before it is referenced.
                      > It could well still be "undefined". And that would be equally bad.
                      >
                      > > If you disagree, I completely respect that. However, given his
                      > > experience, and the fact that he wrote a lint program in JavaScript,
                      > > I tend to follow his advice.
                      >
                      > I don't disagree. Quite the reverse. It's why I'm concerned that
                      > your solution doesn't fix the real problem, it just makes the warning
                      > go away. And there are plenty of cases where that's not safe.
                      >
                      > Am I making more sense now?
                      >
                      > --jhawk@...
                      > John Hawkinson
                      >



                      [Non-text portions of this message have been removed]
                    • John Hawkinson
                      Kent Davidson wrote on Thu, 10 Mar 2011 ... But making the proposed change is no safer than the original code! I agree the warning is
                      Message 10 of 24 , Mar 10 6:55 AM
                      • 0 Attachment
                        Kent Davidson <kent@...> wrote on Thu, 10 Mar 2011
                        at 09:18:32 -0500 in <A45E4A5D-C2C5-43FA-922F-C23C6DBE1D2B@...>:

                        > Hm. Well. I think so, but you say you want to write it a certain
                        > way, yet that way is flagged as an error. So, if you agree with
                        > JSLint, then you'd change the code.

                        But making the proposed change is no safer than the original code!
                        I agree the warning is right.
                        Your 3rd solution is better, though, thank you!

                        To summarize, (in abbreviated form; assume a function() wrapper),
                        JSLint warns us about:

                        var foo = bar(function() { foo.m(); }); // Case 1

                        You suggested the solution is to use:

                        var foo;
                        foo = bar(function() { foo.m(); }); // Case 2

                        I'm concerned that doesn't solve anything other than JSLint's warning.
                        If foo() might have been invalid in Case 1, it's just as invalid in Case 2.

                        A future version of JSLint would be just as right to warn about Case 2
                        as Case 1.

                        I agree the better solution is to use:

                        var foo;
                        foo = bar(function() { foo && foo.m(); }); // Case 3

                        Except JSLint doesn't like the guard operator on function calls
                        ("Expected an assignment or function call and instead saw an expression.")
                        so we must write:

                        var foo;
                        foo = bar(function() { if (foo) { foo.m(); } }); // Case 4

                        I do have to say, it makes me sad that the in the name of avoiding standalone
                        expressions, we have to add an extra level of braces that make the code
                        harder to read.

                        => I wonder if its asking too much for JSLint to permit
                        the guard operator (&&) on function calls. Thoughts?




                        Actually, you proposed:

                        var foo = null;
                        foo = bar(function() { if (foo) { foo.m(); } }); // Case 5

                        but why bother to initialize foo to null? The truthiness of null and
                        and undefined is the same...

                        > My point previously was that regardless of warnings or not, if a
                        > small tweak in my code will eliminate the warning, I err on the side
                        > of just making the cosmetic change, even if I know the warning

                        Looks like you didn't finish that sentence...

                        --jhawk@...
                        John Hawkinson
                      • Kent Davidson
                        ... True, it s not necessary in this case. ... I err on the side of just making the cosmetic change, even if I know the warning is not really going to affect
                        Message 11 of 24 , Mar 10 8:32 AM
                        • 0 Attachment
                          On Mar 10, 2011, at 9:55 AM, John Hawkinson wrote:

                          > but why bother to initialize foo to null? The truthiness of null and
                          > and undefined is the same...

                          True, it's not necessary in this case.

                          > > My point previously was that regardless of warnings or not, if a
                          > > small tweak in my code will eliminate the warning, I err on the side
                          > > of just making the cosmetic change, even if I know the warning
                          >
                          > Looks like you didn't finish that sentence...

                          I err on the side of just making the cosmetic change, even if I know the warning is not really going to affect the code.

                          > I do have to say, it makes me sad that the in the name of avoiding standalone
                          > expressions, we have to add an extra level of braces that make the code
                          > harder to read.

                          Anonymous functions are generally hard to read, esp. when on the same line. I use http://prettydiff.com if presented with something which is hard to read. It's another awesome tool to make code more readable.

                          foo && foo.m()

                          is a nice construct, but the assumption is that jslint is trying to avoid the issue where you want to assign something, but forgot to. So,

                          var x = foo && foo.m();

                          validates, but

                          foo && foo.m()

                          doesn't. It could be an error if you actually did want to use the expression result, which && returns.

                          And:

                          if (foo) {
                          foo.m();
                          }

                          returns nothing, which is what is wanted in this case.

                          As for harder to read, we're really nitting picks at this point.

                          Cheers,

                          -Kent.
                          --
                          Kent Davidson
                          Market Ruler, LLC
                          Marketing Power Tools
                          http://marketruler.com/?_cr=efoot
                          +1 866-622-8636 x88



                          [Non-text portions of this message have been removed]
                        • abyssoft@ymail.com
                          In response to the last question, ... ********** The Truthiness of null and undefined are not the same. while null == undefined is true, null === undefined is
                          Message 12 of 24 , Mar 10 8:53 AM
                          • 0 Attachment
                            In response to the last question,

                            **********
                            --- In jslint_com@yahoogroups.com, John Hawkinson <jhawk@...> wrote:
                            > Actually, you proposed:
                            >
                            > var foo = null;
                            > foo = bar(function() { if (foo) { foo.m(); } }); // Case 5
                            >
                            > but why bother to initialize foo to null? The truthiness of null and
                            > and undefined is the same...
                            **********

                            The Truthiness of null and undefined are not the same.
                            while null == undefined is true, null === undefined is false; if the Truthiness was the same then null === undefined would be true.
                            Also the intent of null and undefined are different in nature. When a variable is declared it is by definition is undefined. Which is to say it is not empty, but rather that it has yet to be defined. Null is a empty state, think of Null in terms of math with help with this concept as null is also known as the empty set. Undefined in math terms and null (empty) in math terms are not equivalent (undefined !== null) but they are similar (undefined == null).


                            Declare but leave uninitialized

                            var someVariable1; // assignment to undefined is understood
                            var someVariable2 = undefined; // does the same as example 1 but will throw lint warning, use example 1

                            someVariable1 = 123; // assign value to variable
                            someVariable1 = undefined; // destruct variable but leave declared

                            Declare and initialize to empty
                            var someVariable3 = null;
                            someVariable1 = 123; // assign value to variable
                            someVariable1 = null; // empty variable


                            Hope this short post clears up confusion between undefined and null.
                          • Satyam
                            I would like to add that while undefined means I don t have a clue what the value might be , null means I know it has no value (a known unknown :-P ).
                            Message 13 of 24 , Mar 10 10:10 AM
                            • 0 Attachment
                              I would like to add that while undefined means "I don't have a clue what
                              the value might be", null means "I know it has no value" (a known
                              unknown :-P ). Beyond a certain point in the application, undefined
                              might mean there is an error, null means I know it has no value.

                              If more languages had null and undefined we wouldn't have silly
                              conventions like indexOf returning -1 when an item was not found. In
                              JavaScript it is an unfortunate legacy of other languages. Ideally, you
                              could have:

                              var position = 'abc'.indexOf('a') sets position to 0
                              var position = 'abc'.indexOf('x') should set position to null, not -1
                              var position = null.indexOf('a') should leave position undefined
                              (assuming the error was not caught)

                              All those (0, null and undefined) are falsy but they are far from
                              meaning the same. The last one shows why I said that a variable set to
                              undefined might signal an error when found beyond a certain point.

                              In the second case, returning false would also be an arbitrary
                              convention, just as -1 is. It might be agreed that false means 'it is
                              not there' but indexOf asks for the position of the item sought, not if
                              it is there (where false would be a logical reply) and if it is not
                              there, it has no position, no such thing, thus null, not false. Anyway,
                              this is beyond the point.

                              Satyam


                              El 10/03/2011 17:53, abyssoft@... escribió:
                              >
                              > In response to the last question,
                              >
                              > **********
                              > --- In jslint_com@yahoogroups.com
                              > <mailto:jslint_com%40yahoogroups.com>, John Hawkinson <jhawk@...> wrote:
                              > > Actually, you proposed:
                              > >
                              > > var foo = null;
                              > > foo = bar(function() { if (foo) { foo.m(); } }); // Case 5
                              > >
                              > > but why bother to initialize foo to null? The truthiness of null and
                              > > and undefined is the same...
                              > **********
                              >
                              > The Truthiness of null and undefined are not the same.
                              > while null == undefined is true, null === undefined is false; if the
                              > Truthiness was the same then null === undefined would be true.
                              > Also the intent of null and undefined are different in nature. When a
                              > variable is declared it is by definition is undefined. Which is to say
                              > it is not empty, but rather that it has yet to be defined. Null is a
                              > empty state, think of Null in terms of math with help with this
                              > concept as null is also known as the empty set. Undefined in math
                              > terms and null (empty) in math terms are not equivalent (undefined !==
                              > null) but they are similar (undefined == null).
                              >
                              > Declare but leave uninitialized
                              >
                              > var someVariable1; // assignment to undefined is understood
                              > var someVariable2 = undefined; // does the same as example 1 but will
                              > throw lint warning, use example 1
                              >
                              > someVariable1 = 123; // assign value to variable
                              > someVariable1 = undefined; // destruct variable but leave declared
                              >
                              > Declare and initialize to empty
                              > var someVariable3 = null;
                              > someVariable1 = 123; // assign value to variable
                              > someVariable1 = null; // empty variable
                              >
                              > Hope this short post clears up confusion between undefined and null.
                              >
                              >
                              > ------------------------------------------------------------------------
                              >
                              > No virus found in this message.
                              > Checked by AVG - www.avg.com <http://www.avg.com>
                              > Version: 10.0.1204 / Virus Database: 1497/3495 - Release Date: 03/09/11
                              >


                              [Non-text portions of this message have been removed]
                            • Lindsay John Lawrence
                              Hello,       Problem at line 9 character 7: Unexpected continue . I now get an error like the above with loops that have a continue of the form shown
                              Message 14 of 24 , Mar 10 10:50 AM
                              • 0 Attachment
                                Hello,

                                      Problem at line 9 character 7: Unexpected 'continue'.


                                I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.

                                Is this a bug in jslint or is it now enforcing some form I am not aware of?

                                // --- start sample code ---
                                var nop = function () {
                                  return;
                                };

                                var test = function () {
                                  var i;
                                  for (i=0; i<10; i+=1) {
                                    if (i<5) {
                                      continue;
                                    }
                                    nop();
                                  }
                                };
                                // --- end sample code ---

                                Thanks in advancet,

                                Lindsay





                                [Non-text portions of this message have been removed]
                              • spence.randall@ymail.com
                                It s not a bug, JSLint has a new(ish) Tolerate continue option. You can check that box in options, or add /*jslint continue:true*/ to your code. Or you could
                                Message 15 of 24 , Mar 10 11:00 AM
                                • 0 Attachment
                                  It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.

                                  Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().

                                  -Randall

                                  --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:
                                  >
                                  > Hello,
                                  >
                                  >       Problem at line 9 character 7: Unexpected 'continue'.
                                  >
                                  >
                                  > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.
                                  >
                                  > Is this a bug in jslint or is it now enforcing some form I am not aware of?
                                  >
                                  > // --- start sample code ---
                                  > var nop = function () {
                                  >   return;
                                  > };
                                  >
                                  > var test = function () {
                                  >   var i;
                                  >   for (i=0; i<10; i+=1) {
                                  >     if (i<5) {
                                  >       continue;
                                  >     }
                                  >     nop();
                                  >   }
                                  > };
                                  > // --- end sample code ---
                                  >
                                  > Thanks in advancet,
                                  >
                                  > Lindsay
                                  >
                                  >
                                  >
                                  >
                                  >
                                  > [Non-text portions of this message have been removed]
                                  >
                                • Lindsay John Lawrence
                                  Thanks! Why the change though? Is continue being deprecated in the language? --Lindsay ... From: spence.randall@ymail.com Subject:
                                  Message 16 of 24 , Mar 10 11:34 AM
                                  • 0 Attachment
                                    Thanks! Why the change though? Is 'continue' being deprecated in the language?

                                    --Lindsay


                                    --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:

                                    From: spence.randall@... <randall@...>
                                    Subject: [jslint] Re: JSLint Bug?
                                    To: jslint_com@yahoogroups.com
                                    Date: Thursday, March 10, 2011, 11:00 AM







                                     













                                    It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.



                                    Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().



                                    -Randall



                                    --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:

                                    >

                                    > Hello,

                                    >

                                    >       Problem at line 9 character 7: Unexpected 'continue'.

                                    >

                                    >

                                    > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.

                                    >

                                    > Is this a bug in jslint or is it now enforcing some form I am not aware of?

                                    >

                                    > // --- start sample code ---

                                    > var nop = function () {

                                    >   return;

                                    > };

                                    >

                                    > var test = function () {

                                    >   var i;

                                    >   for (i=0; i<10; i+=1) {

                                    >     if (i<5) {

                                    >       continue;

                                    >     }

                                    >     nop();

                                    >   }

                                    > };

                                    > // --- end sample code ---

                                    >

                                    > Thanks in advancet,

                                    >

                                    > Lindsay

                                    >

                                    >

                                    >

                                    >

                                    >

                                    > [Non-text portions of this message have been removed]

                                    >

























                                    [Non-text portions of this message have been removed]
                                  • spence.randall@ymail.com
                                    Deprecated? No, I don t think so. There are almost always better ways of writing statements that more explicitly define what you are attempting to do without
                                    Message 17 of 24 , Mar 10 11:47 AM
                                    • 0 Attachment
                                      Deprecated? No, I don't think so. There are almost always better ways of writing statements that more explicitly define what you are attempting to do without resorting to continue. JSLint is all about the good parts, and not about the parts that are acceptable. It forces you to use a higher standard than the one defined.

                                      Douglas says it best in his book:

                                      "The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement."

                                      -Randall

                                      --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:
                                      >
                                      > Thanks! Why the change though? Is 'continue' being deprecated in the language?
                                      >
                                      > --Lindsay
                                      >
                                      >
                                      > --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:
                                      >
                                      > From: spence.randall@... <randall@...>
                                      > Subject: [jslint] Re: JSLint Bug?
                                      > To: jslint_com@yahoogroups.com
                                      > Date: Thursday, March 10, 2011, 11:00 AM
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >  
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      > It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.
                                      >
                                      >
                                      >
                                      > Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().
                                      >
                                      >
                                      >
                                      > -Randall
                                      >
                                      >
                                      >
                                      > --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@> wrote:
                                      >
                                      > >
                                      >
                                      > > Hello,
                                      >
                                      > >
                                      >
                                      > >       Problem at line 9 character 7: Unexpected 'continue'.
                                      >
                                      > >
                                      >
                                      > >
                                      >
                                      > > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.
                                      >
                                      > >
                                      >
                                      > > Is this a bug in jslint or is it now enforcing some form I am not aware of?
                                      >
                                      > >
                                      >
                                      > > // --- start sample code ---
                                      >
                                      > > var nop = function () {
                                      >
                                      > >   return;
                                      >
                                      > > };
                                      >
                                      > >
                                      >
                                      > > var test = function () {
                                      >
                                      > >   var i;
                                      >
                                      > >   for (i=0; i<10; i+=1) {
                                      >
                                      > >     if (i<5) {
                                      >
                                      > >       continue;
                                      >
                                      > >     }
                                      >
                                      > >     nop();
                                      >
                                      > >   }
                                      >
                                      > > };
                                      >
                                      > > // --- end sample code ---
                                      >
                                      > >
                                      >
                                      > > Thanks in advancet,
                                      >
                                      > >
                                      >
                                      > > Lindsay
                                      >
                                      > >
                                      >
                                      > >
                                      >
                                      > >
                                      >
                                      > >
                                      >
                                      > >
                                      >
                                      > > [Non-text portions of this message have been removed]
                                      >
                                      > >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      >
                                      > [Non-text portions of this message have been removed]
                                      >
                                    • Lindsay John Lawrence
                                      Thanks! I have read that book cover to cover several times and it continues to be an excellent reference... somehow I missed that statement of his though. ...
                                      Message 18 of 24 , Mar 10 3:06 PM
                                      • 0 Attachment
                                        Thanks! I have read that book cover to cover several times and it continues to be an excellent reference... somehow I missed that statement of his though.

                                        --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:

                                        From: spence.randall@... <randall@...>
                                        Subject: [jslint] Re: JSLint Bug?
                                        To: jslint_com@yahoogroups.com
                                        Date: Thursday, March 10, 2011, 11:47 AM







                                         













                                        Deprecated? No, I don't think so. There are almost always better ways of writing statements that more explicitly define what you are attempting to do without resorting to continue. JSLint is all about the good parts, and not about the parts that are acceptable. It forces you to use a higher standard than the one defined.



                                        Douglas says it best in his book:



                                        "The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement."



                                        -Randall



                                        --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:

                                        >

                                        > Thanks! Why the change though? Is 'continue' being deprecated in the language?

                                        >

                                        > --Lindsay

                                        >

                                        >

                                        > --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:

                                        >

                                        > From: spence.randall@... <randall@...>

                                        > Subject: [jslint] Re: JSLint Bug?

                                        > To: jslint_com@yahoogroups.com

                                        > Date: Thursday, March 10, 2011, 11:00 AM

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >  

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        > It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.

                                        >

                                        >

                                        >

                                        > Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().

                                        >

                                        >

                                        >

                                        > -Randall

                                        >

                                        >

                                        >

                                        > --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@> wrote:

                                        >

                                        > >

                                        >

                                        > > Hello,

                                        >

                                        > >

                                        >

                                        > >       Problem at line 9 character 7: Unexpected 'continue'.

                                        >

                                        > >

                                        >

                                        > >

                                        >

                                        > > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.

                                        >

                                        > >

                                        >

                                        > > Is this a bug in jslint or is it now enforcing some form I am not aware of?

                                        >

                                        > >

                                        >

                                        > > // --- start sample code ---

                                        >

                                        > > var nop = function () {

                                        >

                                        > >   return;

                                        >

                                        > > };

                                        >

                                        > >

                                        >

                                        > > var test = function () {

                                        >

                                        > >   var i;

                                        >

                                        > >   for (i=0; i<10; i+=1) {

                                        >

                                        > >     if (i<5) {

                                        >

                                        > >       continue;

                                        >

                                        > >     }

                                        >

                                        > >     nop();

                                        >

                                        > >   }

                                        >

                                        > > };

                                        >

                                        > > // --- end sample code ---

                                        >

                                        > >

                                        >

                                        > > Thanks in advancet,

                                        >

                                        > >

                                        >

                                        > > Lindsay

                                        >

                                        > >

                                        >

                                        > >

                                        >

                                        > >

                                        >

                                        > >

                                        >

                                        > >

                                        >

                                        > > [Non-text portions of this message have been removed]

                                        >

                                        > >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        >

                                        > [Non-text portions of this message have been removed]

                                        >

























                                        [Non-text portions of this message have been removed]
                                      Your message has been successfully submitted and would be delivered to recipients shortly.