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

Re: typeof null

Expand Messages
  • spence.randall@ymail.com
    I foresee a minor problem with this: in most browsers, undefined is not read-only and can be overwritten, which means if you have other code on a page outside
    Message 1 of 10 , Jul 19, 2012
    • 0 Attachment
      I foresee a minor problem with this: in most browsers, undefined is not read-only and can be overwritten, which means if you have other code on a page outside of your control, it's safer to do a typeof === 'undefined' check rather than x === undefined.

      For example:

      var undefined = 1,
      x;
      alert(x === undefined);

      Will alert false, whereas

      var undefined = 1,
      x;
      alert(typeof x === undefined);

      will alert true, as expected.

      Note, only Firefox seems to throw an error saying undefined is read-only.

      Granted it is irresponsible and insanely stupid to redefine undefined, but their are some irresponsible and insanely stupid people out in the wild writing code, the later test at least seems more bullet proof.

      Thanks,
      Randall

      --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
      >
      > JSLint will now warn if you write
      >
      > typeof x === 'null' || typeof x === 'undefined'
      >
      > instead of
      >
      > x === null || x === undefined
      >
    • [MACSkeptic] Mozair Alves do Carmo Júnio
      If you are concerned that undefined might be redefined, you can always wrap your code with a self invoking function, right? Something like: (function
      Message 2 of 10 , Jul 19, 2012
      • 0 Attachment
        If you are concerned that undefined might be redefined, you can always wrap
        your code with a self invoking function, right?

        Something like:

        (function (undefined) {

        } ());

        On Thu, Jul 19, 2012 at 1:10 PM, spence.randall@... <
        randall@...> wrote:

        > **
        >
        >
        >
        >
        > I foresee a minor problem with this: in most browsers, undefined is not
        > read-only and can be overwritten, which means if you have other code on a
        > page outside of your control, it's safer to do a typeof === 'undefined'
        > check rather than x === undefined.
        >
        > For example:
        >
        > var undefined = 1,
        > x;
        > alert(x === undefined);
        >
        > Will alert false, whereas
        >
        > var undefined = 1,
        > x;
        > alert(typeof x === undefined);
        >
        > will alert true, as expected.
        >
        > Note, only Firefox seems to throw an error saying undefined is read-only.
        >
        > Granted it is irresponsible and insanely stupid to redefine undefined, but
        > their are some irresponsible and insanely stupid people out in the wild
        > writing code, the later test at least seems more bullet proof.
        >
        > Thanks,
        > Randall
        >
        >
        > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
        > >
        > > JSLint will now warn if you write
        > >
        > > typeof x === 'null' || typeof x === 'undefined'
        > >
        > > instead of
        > >
        > > x === null || x === undefined
        > >
        >
        >
        >


        [Non-text portions of this message have been removed]
      • Michael
        JSLint will complain if you try to redefine undefined: Read only. undefined = foo ; On Thu, Jul 19, 2012 at 12:26 PM, [MACSkeptic] Mozair Alves do Carmo
        Message 3 of 10 , Jul 19, 2012
        • 0 Attachment
          JSLint will complain if you try to redefine undefined:
          Read only.


          undefined = "foo";




          On Thu, Jul 19, 2012 at 12:26 PM, [MACSkeptic] Mozair Alves do Carmo Júnior
          <macskeptic@...> wrote:

          > If you are concerned that undefined might be redefined, you can always wrap
          > your code with a self invoking function, right?
          >
          > Something like:
          >
          > (function (undefined) {
          >
          > } ());
          >
          > On Thu, Jul 19, 2012 at 1:10 PM, spence.randall@... <
          > randall@...> wrote:
          >
          > > **
          > >
          > >
          > >
          > >
          > > I foresee a minor problem with this: in most browsers, undefined is not
          > > read-only and can be overwritten, which means if you have other code on a
          > > page outside of your control, it's safer to do a typeof === 'undefined'
          > > check rather than x === undefined.
          > >
          > > For example:
          > >
          > > var undefined = 1,
          > > x;
          > > alert(x === undefined);
          > >
          > > Will alert false, whereas
          > >
          > > var undefined = 1,
          > > x;
          > > alert(typeof x === undefined);
          > >
          > > will alert true, as expected.
          > >
          > > Note, only Firefox seems to throw an error saying undefined is read-only.
          > >
          > > Granted it is irresponsible and insanely stupid to redefine undefined,
          > but
          > > their are some irresponsible and insanely stupid people out in the wild
          > > writing code, the later test at least seems more bullet proof.
          > >
          > > Thanks,
          > > Randall
          > >
          > >
          > > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...>
          > wrote:
          > > >
          > > > JSLint will now warn if you write
          > > >
          > > > typeof x === 'null' || typeof x === 'undefined'
          > > >
          > > > instead of
          > > >
          > > > x === null || x === undefined
          > > >
          > >
          > >
          > >
          >
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >
          > ------------------------------------
          >
          > Yahoo! Groups Links
          >
          >
          >
          >


          [Non-text portions of this message have been removed]
        • Rob Richardson
          If I am building a library and someone stupid consumes my library and also redefines undefined in a very foolish way, and my library breaks because of it, the
          Message 4 of 10 , Jul 19, 2012
          • 0 Attachment
            If I am building a library and someone stupid consumes my library and also
            redefines undefined in a very foolish way, and my library breaks because of
            it, the initial blame will be on my library, not on the redefinition of
            undefined. The same argument in a different venue directs the use of
            x.hasOwnProperty(y) -- it's only necessary if someone foolishly changes
            content in the prototype chain. For the same reason that I always start a
            script with a semicolon to avoid a previous bad library concatenated into
            mine from ruining my functionality, coding defensively against stupid uses
            of undefined is a staple, and should be used liberally. A self-invoking
            function with one more argument than is passed is the perfect way to get a
            local undefined with the correct value (and avoids the global namespace
            lookup). Provided I can carefully control closure execution, I can maintain
            this local understanding of undefined. Because (typeof x === 'undefined')
            always returns the correct response in spite of the value of a variable by
            the same name, this also seems a more defensive and safer strategy.

            If you're really trying to dissuade use of this:

            if(typeof x === 'undefined' || typeof x === 'null') {

            perhaps you should point us to the brilliantly simple answer that avoids it
            all:

            if(!x) {

            (granted "truthy" and "not null or undefined" are indeed different for 0,
            "", etc.)

            I'd argue that most of the time, we're not checking for null or checking for
            undefined but rather we are checking for initialization. Provided we
            understand truthy and falsey correctly, 'if(!x) {' is much more expressive
            than 'if(x === undefined || x === nu...I already stopped reading your code'.
            Just ban the use of comparison to null and undefined. Or even further, like
            the "TODO:" check, flag any code that includes the words "undefined" or
            "null", providing examples like 'if(!x) {' and 'if(!!x) {' in the error
            description and documentation.

            Rob


            -----Original Message-----
            From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
            Behalf Of Michael
            Sent: Thursday, July 19, 2012 9:29 AM
            To: jslint_com@yahoogroups.com
            Subject: Re: [jslint] Re: typeof null



            JSLint will complain if you try to redefine undefined:
            Read only.

            undefined = "foo";

            On Thu, Jul 19, 2012 at 12:26 PM, [MACSkeptic] Mozair Alves do Carmo Júnior
            <macskeptic@... <mailto:macskeptic%40gmail.com> > wrote:

            > If you are concerned that undefined might be redefined, you can always
            > wrap your code with a self invoking function, right?
            >
            > Something like:
            >
            > (function (undefined) {
            >
            > } ());
            >
            > On Thu, Jul 19, 2012 at 1:10 PM, spence.randall@...
            > <mailto:spence.randall%40ymail.com> < randall@...
            <mailto:randall%40analogueweb.com> > wrote:
            >
            > > **
            > >
            > >
            > >
            > >
            > > I foresee a minor problem with this: in most browsers, undefined is
            > > not read-only and can be overwritten, which means if you have other
            > > code on a page outside of your control, it's safer to do a typeof ===
            'undefined'
            > > check rather than x === undefined.
            > >
            > > For example:
            > >
            > > var undefined = 1,
            > > x;
            > > alert(x === undefined);
            > >
            > > Will alert false, whereas
            > >
            > > var undefined = 1,
            > > x;
            > > alert(typeof x === undefined);
            > >
            > > will alert true, as expected.
            > >
            > > Note, only Firefox seems to throw an error saying undefined is
            read-only.
            > >
            > > Granted it is irresponsible and insanely stupid to redefine
            > > undefined,
            > but
            > > their are some irresponsible and insanely stupid people out in the
            > > wild writing code, the later test at least seems more bullet proof.
            > >
            > > Thanks,
            > > Randall
            > >
            > >
            > > --- In jslint_com@yahoogroups.com
            > > <mailto:jslint_com%40yahoogroups.com> , "douglascrockford"
            > > <douglas@...>
            > wrote:
            > > >
            > > > JSLint will now warn if you write
            > > >
            > > > typeof x === 'null' || typeof x === 'undefined'
            > > >
            > > > instead of
            > > >
            > > > x === null || x === undefined
            > > >
            > >
            > >
            > >
            >
            >
            > [Non-text portions of this message have been removed]
            >
            >
            >
            > ------------------------------------
            >
            > Yahoo! Groups Links
            >
            >
            >
            >

            [Non-text portions of this message have been removed]
          • spence.randall@ymail.com
            I d like to hear from Douglas as to why the change and warning, we still don t know the reason for the change, so perhaps there is something we are missing.
            Message 5 of 10 , Jul 21, 2012
            • 0 Attachment
              I'd like to hear from Douglas as to why the change and warning, we still don't know the reason for the change, so perhaps there is something we are missing.

              Thanks,
              Randall

              --- In jslint_com@yahoogroups.com, "Rob Richardson" <erobrich@...> wrote:
              >
              > If I am building a library and someone stupid consumes my library and also
              > redefines undefined in a very foolish way, and my library breaks because of
              > it, the initial blame will be on my library, not on the redefinition of
              > undefined. The same argument in a different venue directs the use of
              > x.hasOwnProperty(y) -- it's only necessary if someone foolishly changes
              > content in the prototype chain. For the same reason that I always start a
              > script with a semicolon to avoid a previous bad library concatenated into
              > mine from ruining my functionality, coding defensively against stupid uses
              > of undefined is a staple, and should be used liberally. A self-invoking
              > function with one more argument than is passed is the perfect way to get a
              > local undefined with the correct value (and avoids the global namespace
              > lookup). Provided I can carefully control closure execution, I can maintain
              > this local understanding of undefined. Because (typeof x === 'undefined')
              > always returns the correct response in spite of the value of a variable by
              > the same name, this also seems a more defensive and safer strategy.
              >
              > If you're really trying to dissuade use of this:
              >
              > if(typeof x === 'undefined' || typeof x === 'null') {
              >
              > perhaps you should point us to the brilliantly simple answer that avoids it
              > all:
              >
              > if(!x) {
              >
              > (granted "truthy" and "not null or undefined" are indeed different for 0,
              > "", etc.)
              >
              > I'd argue that most of the time, we're not checking for null or checking for
              > undefined but rather we are checking for initialization. Provided we
              > understand truthy and falsey correctly, 'if(!x) {' is much more expressive
              > than 'if(x === undefined || x === nu...I already stopped reading your code'.
              > Just ban the use of comparison to null and undefined. Or even further, like
              > the "TODO:" check, flag any code that includes the words "undefined" or
              > "null", providing examples like 'if(!x) {' and 'if(!!x) {' in the error
              > description and documentation.
              >
              > Rob
              >
              >
              > -----Original Message-----
              > From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
              > Behalf Of Michael
              > Sent: Thursday, July 19, 2012 9:29 AM
              > To: jslint_com@yahoogroups.com
              > Subject: Re: [jslint] Re: typeof null
              >
              >
              >
              > JSLint will complain if you try to redefine undefined:
              > Read only.
              >
              > undefined = "foo";
              >
              > On Thu, Jul 19, 2012 at 12:26 PM, [MACSkeptic] Mozair Alves do Carmo Júnior
              > <macskeptic@... <mailto:macskeptic%40gmail.com> > wrote:
              >
              > > If you are concerned that undefined might be redefined, you can always
              > > wrap your code with a self invoking function, right?
              > >
              > > Something like:
              > >
              > > (function (undefined) {
              > >
              > > } ());
              > >
              > > On Thu, Jul 19, 2012 at 1:10 PM, spence.randall@...
              > > <mailto:spence.randall%40ymail.com> < randall@...
              > <mailto:randall%40analogueweb.com> > wrote:
              > >
              > > > **
              > > >
              > > >
              > > >
              > > >
              > > > I foresee a minor problem with this: in most browsers, undefined is
              > > > not read-only and can be overwritten, which means if you have other
              > > > code on a page outside of your control, it's safer to do a typeof ===
              > 'undefined'
              > > > check rather than x === undefined.
              > > >
              > > > For example:
              > > >
              > > > var undefined = 1,
              > > > x;
              > > > alert(x === undefined);
              > > >
              > > > Will alert false, whereas
              > > >
              > > > var undefined = 1,
              > > > x;
              > > > alert(typeof x === undefined);
              > > >
              > > > will alert true, as expected.
              > > >
              > > > Note, only Firefox seems to throw an error saying undefined is
              > read-only.
              > > >
              > > > Granted it is irresponsible and insanely stupid to redefine
              > > > undefined,
              > > but
              > > > their are some irresponsible and insanely stupid people out in the
              > > > wild writing code, the later test at least seems more bullet proof.
              > > >
              > > > Thanks,
              > > > Randall
              > > >
              > > >
              > > > --- In jslint_com@yahoogroups.com
              > > > <mailto:jslint_com%40yahoogroups.com> , "douglascrockford"
              > > > <douglas@>
              > > wrote:
              > > > >
              > > > > JSLint will now warn if you write
              > > > >
              > > > > typeof x === 'null' || typeof x === 'undefined'
              > > > >
              > > > > instead of
              > > > >
              > > > > x === null || x === undefined
              > > > >
              > > >
              > > >
              > > >
              > >
              > >
              > > [Non-text portions of this message have been removed]
              > >
              > >
              > >
              > > ------------------------------------
              > >
              > > Yahoo! Groups Links
              > >
              > >
              > >
              > >
              >
              > [Non-text portions of this message have been removed]
              >
            • douglascrockford
              ... typeof null is an error. typeof undefined is bigger and slower and less informative.
              Message 6 of 10 , Jul 23, 2012
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, "spence.randall@..." <randall@...> wrote:
                >
                > I'd like to hear from Douglas as to why the change and warning, we still don't know the reason for the change, so perhaps there is something we are missing.


                typeof null is an error.
                typeof undefined is bigger and slower and less informative.
              • Joe Hansche
                ... still don t know the reason for the change, so perhaps there is something we are missing. ... To clarify: see
                Message 7 of 10 , Jul 23, 2012
                • 0 Attachment
                  On Jul 23, 2012 12:57 PM, "douglascrockford" <douglas@...> wrote:
                  >
                  >
                  >
                  > --- In jslint_com@yahoogroups.com, "spence.randall@..." <randall@...>
                  wrote:
                  > >
                  > > I'd like to hear from Douglas as to why the change and warning, we
                  still don't know the reason for the change, so perhaps there is something
                  we are missing.
                  >
                  > typeof null is an error.

                  To clarify: see
                  https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof#null

                  typeof null returns "object", which is counter productive, and will become
                  inconsistent in future versions of ECMAScript.

                  > typeof undefined is bigger and slower and less informative.
                  >
                  >


                  [Non-text portions of this message have been removed]
                • spence.randall@ymail.com
                  Right, typof null was never a question, that one is pretty well known to be problematic. I was more curious about typeof undefined. So typeof undefined is
                  Message 8 of 10 , Jul 23, 2012
                  • 0 Attachment
                    Right, typof null was never a question, that one is pretty well known to be problematic. I was more curious about typeof undefined.

                    So typeof undefined is bigger and slower and less informative, that I can understand, but what do you recommend to guard against someone assigning to undefined? Use our own local variable? That also seems to me like it would be less informative.

                    Thanks,
                    Randall

                    --- In jslint_com@yahoogroups.com, Joe Hansche <madcoder@...> wrote:
                    >
                    > On Jul 23, 2012 12:57 PM, "douglascrockford" <douglas@...> wrote:
                    > >
                    > >
                    > >
                    > > --- In jslint_com@yahoogroups.com, "spence.randall@" <randall@>
                    > wrote:
                    > > >
                    > > > I'd like to hear from Douglas as to why the change and warning, we
                    > still don't know the reason for the change, so perhaps there is something
                    > we are missing.
                    > >
                    > > typeof null is an error.
                    >
                    > To clarify: see
                    > https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof#null
                    >
                    > typeof null returns "object", which is counter productive, and will become
                    > inconsistent in future versions of ECMAScript.
                    >
                    > > typeof undefined is bigger and slower and less informative.
                    > >
                    > >
                    >
                    >
                    > [Non-text portions of this message have been removed]
                    >
                  • douglascrockford
                    ... Let me know when that happens.
                    Message 9 of 10 , Jul 23, 2012
                    • 0 Attachment
                      --- In jslint_com@yahoogroups.com, "spence.randall@..." <randall@...> wrote:
                      >
                      > Right, typof null was never a question, that one is pretty well known to be problematic. I was more curious about typeof undefined.
                      >
                      > So typeof undefined is bigger and slower and less informative, that I can understand, but what do you recommend to guard against someone assigning to undefined? Use our own local variable? That also seems to me like it would be less informative.


                      Let me know when that happens.
                    Your message has been successfully submitted and would be delivered to recipients shortly.