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

Re: (typeof x === 'undefined') !== (x === undefined)

Expand Messages
  • s_lubowsky
    OK, but now you have to wonder if this is an improvement. The old code would have worked as is, the new code will fail unless the developer remembered to add
    Message 1 of 7 , Jul 25, 2012
    • 0 Attachment
      OK, but now you have to wonder if this is an improvement. The old code would have worked as is, the new code will fail unless the developer remembered to add this "extra" code (especially likely to "forget" if migrating old code that already had the old style check)

      In any event, thanks for the great tool.
      Stephen

      --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
      >
      > --- In jslint_com@yahoogroups.com, "s_lubowsky" <slubowsky@> wrote:
      > >
      > > While I don't mind the new rule, I have run into a case where the suggested replacement style code doesn't work.
      > >
      > > For example suppose we have a page that uses two different javascript files.
      > >
      > > The first defines an object with some functions, e.g. something like:
      > >
      > > var Foo = {
      > > util: {
      > > bar: function () {
      > > "use strict";
      > > alert('bar called');
      > > }
      > > }
      > > };
      > >
      > > The second tries to use the Foo.util.bar function BUT only if it exists, e.g. something like this:
      > >
      > > function () {
      > > "use strict";
      > > //if (typeof Foo === 'undefined') { // jslint error
      > > //if (Foo === undefined) { // blows up browser
      > > if (typeof Foo !== 'object') {
      > > alert('Foo not loaded!');
      > > return;
      > > }
      > > Foo.util.bar();
      > > };
      > >
      > > When the second function is run without the first file being loaded, the first (commented out) if statement works but is a jslint error, the second (commented out) if statement causes a browser error (i.e. we don't get the "Foo not loaded!" alert) in both Firefox (16.0a2 (2012-07-25)) and Chrome (21.0.1180.49 beta-m) and the third is what I am forced to use to satisfy both jslint and the browser.
      > >
      > > My real use case is of course much more complex, but I think this simplistic demonstration proves that the two coding constructs are NOT equivalent and there are times when typeof x === 'undefined' SHOULD be used.
      >
      >
      >
      > Put
      >
      > var Foo;
      >
      > near the top of the second file.
      >
    • Luke Page
      The extra code makes it safer as you ll never get a reference error and it makes your dependencies clearer. ... [Non-text portions of this message have been
      Message 2 of 7 , Jul 25, 2012
      • 0 Attachment
        The 'extra code' makes it safer as you'll never get a reference error and
        it makes your dependencies clearer.
        On Jul 25, 2012 9:46 PM, "s_lubowsky" <slubowsky@...> wrote:

        > **
        >
        >
        > OK, but now you have to wonder if this is an improvement. The old code
        > would have worked as is, the new code will fail unless the developer
        > remembered to add this "extra" code (especially likely to "forget" if
        > migrating old code that already had the old style check)
        >
        > In any event, thanks for the great tool.
        > Stephen
        >
        > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
        > >
        > > --- In jslint_com@yahoogroups.com, "s_lubowsky" <slubowsky@> wrote:
        > > >
        > > > While I don't mind the new rule, I have run into a case where the
        > suggested replacement style code doesn't work.
        > > >
        > > > For example suppose we have a page that uses two different javascript
        > files.
        > > >
        > > > The first defines an object with some functions, e.g. something like:
        > > >
        > > > var Foo = {
        > > > util: {
        > > > bar: function () {
        > > > "use strict";
        > > > alert('bar called');
        > > > }
        > > > }
        > > > };
        > > >
        > > > The second tries to use the Foo.util.bar function BUT only if it
        > exists, e.g. something like this:
        > > >
        > > > function () {
        > > > "use strict";
        > > > //if (typeof Foo === 'undefined') { // jslint error
        > > > //if (Foo === undefined) { // blows up browser
        > > > if (typeof Foo !== 'object') {
        > > > alert('Foo not loaded!');
        > > > return;
        > > > }
        > > > Foo.util.bar();
        > > > };
        > > >
        > > > When the second function is run without the first file being loaded,
        > the first (commented out) if statement works but is a jslint error, the
        > second (commented out) if statement causes a browser error (i.e. we don't
        > get the "Foo not loaded!" alert) in both Firefox (16.0a2 (2012-07-25)) and
        > Chrome (21.0.1180.49 beta-m) and the third is what I am forced to use to
        > satisfy both jslint and the browser.
        > > >
        > > > My real use case is of course much more complex, but I think this
        > simplistic demonstration proves that the two coding constructs are NOT
        > equivalent and there are times when typeof x === 'undefined' SHOULD be used.
        > >
        > >
        > >
        > > Put
        > >
        > > var Foo;
        > >
        > > near the top of the second file.
        > >
        >
        >
        >


        [Non-text portions of this message have been removed]
      • s_lubowsky
        But the old way is safer in that it wont blow up in my clients face if I forget to add a useless declaration of a variable defined by someone else in a
        Message 3 of 7 , Jul 25, 2012
        • 0 Attachment
          But the old way is safer in that it wont blow up in my clients face if I forget to add a useless declaration of a variable defined by someone else in a different file. Perhaps if jslint would warn me that the variable being compared to undefined is undefined... but isn't that the point of the code being written, to check for undefined?

          Stephen

          --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
          >
          > The 'extra code' makes it safer as you'll never get a reference error and
          > it makes your dependencies clearer.
          > On Jul 25, 2012 9:46 PM, "s_lubowsky" <slubowsky@...> wrote:
          >
          > > **
          > >
          > >
          > > OK, but now you have to wonder if this is an improvement. The old code
          > > would have worked as is, the new code will fail unless the developer
          > > remembered to add this "extra" code (especially likely to "forget" if
          > > migrating old code that already had the old style check)
          > >
          > > In any event, thanks for the great tool.
          > > Stephen
          > >
          > > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@> wrote:
          > > >
          > > > --- In jslint_com@yahoogroups.com, "s_lubowsky" <slubowsky@> wrote:
          > > > >
          > > > > While I don't mind the new rule, I have run into a case where the
          > > suggested replacement style code doesn't work.
          > > > >
          > > > > For example suppose we have a page that uses two different javascript
          > > files.
          > > > >
          > > > > The first defines an object with some functions, e.g. something like:
          > > > >
          > > > > var Foo = {
          > > > > util: {
          > > > > bar: function () {
          > > > > "use strict";
          > > > > alert('bar called');
          > > > > }
          > > > > }
          > > > > };
          > > > >
          > > > > The second tries to use the Foo.util.bar function BUT only if it
          > > exists, e.g. something like this:
          > > > >
          > > > > function () {
          > > > > "use strict";
          > > > > //if (typeof Foo === 'undefined') { // jslint error
          > > > > //if (Foo === undefined) { // blows up browser
          > > > > if (typeof Foo !== 'object') {
          > > > > alert('Foo not loaded!');
          > > > > return;
          > > > > }
          > > > > Foo.util.bar();
          > > > > };
          > > > >
          > > > > When the second function is run without the first file being loaded,
          > > the first (commented out) if statement works but is a jslint error, the
          > > second (commented out) if statement causes a browser error (i.e. we don't
          > > get the "Foo not loaded!" alert) in both Firefox (16.0a2 (2012-07-25)) and
          > > Chrome (21.0.1180.49 beta-m) and the third is what I am forced to use to
          > > satisfy both jslint and the browser.
          > > > >
          > > > > My real use case is of course much more complex, but I think this
          > > simplistic demonstration proves that the two coding constructs are NOT
          > > equivalent and there are times when typeof x === 'undefined' SHOULD be used.
          > > >
          > > >
          > > >
          > > > Put
          > > >
          > > > var Foo;
          > > >
          > > > near the top of the second file.
          > > >
          > >
          > >
          > >
          >
          >
          > [Non-text portions of this message have been removed]
          >
        • Luke Page
          Jslint does warn you unless you told jslint it was a global... ... [Non-text portions of this message have been removed]
          Message 4 of 7 , Jul 25, 2012
          • 0 Attachment
            Jslint does warn you unless you told jslint it was a global...
            On Jul 25, 2012 10:48 PM, "s_lubowsky" <slubowsky@...> wrote:

            > **
            >
            >
            > But the old way is safer in that it wont blow up in my clients face if I
            > forget to add a useless declaration of a variable defined by someone else
            > in a different file. Perhaps if jslint would warn me that the variable
            > being compared to undefined is undefined... but isn't that the point of the
            > code being written, to check for undefined?
            >
            > Stephen
            >
            > --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
            > >
            > > The 'extra code' makes it safer as you'll never get a reference error and
            > > it makes your dependencies clearer.
            > > On Jul 25, 2012 9:46 PM, "s_lubowsky" <slubowsky@...> wrote:
            > >
            > > > **
            > > >
            > > >
            > > > OK, but now you have to wonder if this is an improvement. The old code
            > > > would have worked as is, the new code will fail unless the developer
            > > > remembered to add this "extra" code (especially likely to "forget" if
            > > > migrating old code that already had the old style check)
            > > >
            > > > In any event, thanks for the great tool.
            > > > Stephen
            > > >
            > > > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@>
            > wrote:
            > > > >
            > > > > --- In jslint_com@yahoogroups.com, "s_lubowsky" <slubowsky@> wrote:
            > > > > >
            > > > > > While I don't mind the new rule, I have run into a case where the
            > > > suggested replacement style code doesn't work.
            > > > > >
            > > > > > For example suppose we have a page that uses two different
            > javascript
            > > > files.
            > > > > >
            > > > > > The first defines an object with some functions, e.g. something
            > like:
            > > > > >
            > > > > > var Foo = {
            > > > > > util: {
            > > > > > bar: function () {
            > > > > > "use strict";
            > > > > > alert('bar called');
            > > > > > }
            > > > > > }
            > > > > > };
            > > > > >
            > > > > > The second tries to use the Foo.util.bar function BUT only if it
            > > > exists, e.g. something like this:
            > > > > >
            > > > > > function () {
            > > > > > "use strict";
            > > > > > //if (typeof Foo === 'undefined') { // jslint error
            > > > > > //if (Foo === undefined) { // blows up browser
            > > > > > if (typeof Foo !== 'object') {
            > > > > > alert('Foo not loaded!');
            > > > > > return;
            > > > > > }
            > > > > > Foo.util.bar();
            > > > > > };
            > > > > >
            > > > > > When the second function is run without the first file being
            > loaded,
            > > > the first (commented out) if statement works but is a jslint error, the
            > > > second (commented out) if statement causes a browser error (i.e. we
            > don't
            > > > get the "Foo not loaded!" alert) in both Firefox (16.0a2 (2012-07-25))
            > and
            > > > Chrome (21.0.1180.49 beta-m) and the third is what I am forced to use
            > to
            > > > satisfy both jslint and the browser.
            > > > > >
            > > > > > My real use case is of course much more complex, but I think this
            > > > simplistic demonstration proves that the two coding constructs are NOT
            > > > equivalent and there are times when typeof x === 'undefined' SHOULD be
            > used.
            > > > >
            > > > >
            > > > >
            > > > > Put
            > > > >
            > > > > var Foo;
            > > > >
            > > > > near the top of the second file.
            > > > >
            > > >
            > > >
            > > >
            > >
            > >
            > > [Non-text portions of this message have been removed]
            > >
            >
            >
            >


            [Non-text portions of this message have been removed]
          • s_lubowsky
            Which if course I did, since it is defined in another file. I guess the lesson here is that rather then telling jslint that things are global you should simply
            Message 5 of 7 , Jul 25, 2012
            • 0 Attachment
              Which if course I did, since it is defined in another file. I guess the lesson here is that rather then telling jslint that things are global you should simply declare them instead. Is that indeed a best practice?

              Stephen

              --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
              >
              > Jslint does warn you unless you told jslint it was a global...
              > On Jul 25, 2012 10:48 PM, "s_lubowsky" <slubowsky@...> wrote:
              >
              > > **
              > >
              > >
              > > But the old way is safer in that it wont blow up in my clients face if I
              > > forget to add a useless declaration of a variable defined by someone else
              > > in a different file. Perhaps if jslint would warn me that the variable
              > > being compared to undefined is undefined... but isn't that the point of the
              > > code being written, to check for undefined?
              > >
              > > Stephen
              > >
              > > --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@> wrote:
              > > >
              > > > The 'extra code' makes it safer as you'll never get a reference error and
              > > > it makes your dependencies clearer.
              > > > On Jul 25, 2012 9:46 PM, "s_lubowsky" <slubowsky@> wrote:
              > > >
              > > > > **
              > > > >
              > > > >
              > > > > OK, but now you have to wonder if this is an improvement. The old code
              > > > > would have worked as is, the new code will fail unless the developer
              > > > > remembered to add this "extra" code (especially likely to "forget" if
              > > > > migrating old code that already had the old style check)
              > > > >
              > > > > In any event, thanks for the great tool.
              > > > > Stephen
              > > > >
              > > > > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@>
              > > wrote:
              > > > > >
              > > > > > --- In jslint_com@yahoogroups.com, "s_lubowsky" <slubowsky@> wrote:
              > > > > > >
              > > > > > > While I don't mind the new rule, I have run into a case where the
              > > > > suggested replacement style code doesn't work.
              > > > > > >
              > > > > > > For example suppose we have a page that uses two different
              > > javascript
              > > > > files.
              > > > > > >
              > > > > > > The first defines an object with some functions, e.g. something
              > > like:
              > > > > > >
              > > > > > > var Foo = {
              > > > > > > util: {
              > > > > > > bar: function () {
              > > > > > > "use strict";
              > > > > > > alert('bar called');
              > > > > > > }
              > > > > > > }
              > > > > > > };
              > > > > > >
              > > > > > > The second tries to use the Foo.util.bar function BUT only if it
              > > > > exists, e.g. something like this:
              > > > > > >
              > > > > > > function () {
              > > > > > > "use strict";
              > > > > > > //if (typeof Foo === 'undefined') { // jslint error
              > > > > > > //if (Foo === undefined) { // blows up browser
              > > > > > > if (typeof Foo !== 'object') {
              > > > > > > alert('Foo not loaded!');
              > > > > > > return;
              > > > > > > }
              > > > > > > Foo.util.bar();
              > > > > > > };
              > > > > > >
              > > > > > > When the second function is run without the first file being
              > > loaded,
              > > > > the first (commented out) if statement works but is a jslint error, the
              > > > > second (commented out) if statement causes a browser error (i.e. we
              > > don't
              > > > > get the "Foo not loaded!" alert) in both Firefox (16.0a2 (2012-07-25))
              > > and
              > > > > Chrome (21.0.1180.49 beta-m) and the third is what I am forced to use
              > > to
              > > > > satisfy both jslint and the browser.
              > > > > > >
              > > > > > > My real use case is of course much more complex, but I think this
              > > > > simplistic demonstration proves that the two coding constructs are NOT
              > > > > equivalent and there are times when typeof x === 'undefined' SHOULD be
              > > used.
              > > > > >
              > > > > >
              > > > > >
              > > > > > Put
              > > > > >
              > > > > > var Foo;
              > > > > >
              > > > > > near the top of the second file.
              > > > > >
              > > > >
              > > > >
              > > > >
              > > >
              > > >
              > > > [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.