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

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

Expand Messages
  • Kent Davidson
    Mar 10, 2011
    • 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]
    • Show all 24 messages in this topic