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

Re: Proposal for jslint functionality

Expand Messages
  • Douglas Crockford
    ... If I were to provide such a service, would the jQuery community use it?
    Message 1 of 9 , Jul 15 4:01 PM
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
      >
      > I just saw in a small jQuery plugin I was borrowing for my own use:
      >
      > function checkUndefined(a) {
      > return typeof a === 'undefined';
      > };
      >
      > if (checkUndefined(data)) {
      > return false;
      > }
      >
      > Not only does this seem like unneeded indirection, but is it even best to
      > use the `typeof` operator here?
      >
      > I thought that simply doing the following would be most clear:
      >
      > if (data === undefined) {
      > return false;
      > }
      >
      > Would alerting on this type of unnecessary type checking be useful?
      >
      > Note: I also appreciate JSLint suggesting String(variable) rather than
      > (variable + '').


      If I were to provide such a service, would the jQuery community use it?
    • Erik Eckhardt
      Douglas, I am pretty sure the jQuery writers would not use it. Their library is definitely not written to JSLint standards and I can t see them adopting it any
      Message 2 of 9 , Jul 15 4:49 PM
      • 0 Attachment
        Douglas,

        I am pretty sure the jQuery writers would not use it. Their library is
        definitely not written to JSLint standards and I can't see them adopting it
        any time soon. I haven't looked to see if this has been even discussed.

        I also can't possibly spend the effort to make the core library pass JSLint.
        However, it has had a long time to mature and there is a very large
        community of users to find problems with it. It is already high quality in
        its function (even if its coding standards are not up to JSLint's). It is
        regularly being updated and has a bug reporting and tracking system. I could
        spend a week trying to make one cross-browser solution and still not have
        something as reliable as what's already implemented in the library (one good
        example is the `ready` event indicating the DOM is ready for use).

        The story is different for plugins. They are often written by one person,
        receive little or no maintenance, and are not used by nearly as wide an
        audience who could find problems with the code (if there was even anyone to
        fix it). Thus, I like to run any plugins I use through JSLint to have just a
        little better assurance of quality. I have abandoned the attempt to import
        some plugins into my library because they were so badly written that getting
        them to pass JSLint was more work than just rewriting them.

        That means that JSLint (and such a service as I proposed) are of great value
        to me and other folks like me who do use code from others. While JSLint may
        not be used by the jQuery developers, the huge community of javascript
        developers who use jQuery can benefit from it. I see plugs for JSLint and
        other js code quality tools regularly on sites like stackoverflow.com.

        I wish that I had the time to write everything from scratch, but I don't. I
        must make compromises and use online libraries and occasionally plugins. But
        I also want to at least have some minimum standards of quality.

        Erik

        On Fri, Jul 15, 2011 at 4:01 PM, Douglas Crockford <douglas@...>wrote:

        > **
        >
        >
        > --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
        > >
        > > I just saw in a small jQuery plugin I was borrowing for my own use:
        > >
        > > function checkUndefined(a) {
        > > return typeof a === 'undefined';
        > > };
        > >
        > > if (checkUndefined(data)) {
        > > return false;
        > > }
        > >
        > > Not only does this seem like unneeded indirection, but is it even best to
        > > use the `typeof` operator here?
        > >
        > > I thought that simply doing the following would be most clear:
        > >
        > > if (data === undefined) {
        > > return false;
        > > }
        > >
        > > Would alerting on this type of unnecessary type checking be useful?
        > >
        > > Note: I also appreciate JSLint suggesting String(variable) rather than
        > > (variable + '').
        >
        > If I were to provide such a service, would the jQuery community use it?
        >
        >
        >


        [Non-text portions of this message have been removed]
      • Joshua Bell
        ... In older browsers, the value of window.undefined can be modified; code may wish to be resilient in the face of this.
        Message 3 of 9 , Jul 15 4:52 PM
        • 0 Attachment
          On Fri, Jul 15, 2011 at 3:44 PM, Erik Eckhardt <erik@...> wrote:

          > I just saw in a small jQuery plugin I was borrowing for my own use:
          >
          > function checkUndefined(a) {
          > return typeof a === 'undefined';
          > };
          >
          > if (checkUndefined(data)) {
          > return false;
          > }
          >
          > Not only does this seem like unneeded indirection, but is it even best to
          > use the `typeof` operator here?
          >

          In older browsers, the value of window.undefined can be modified; code may
          wish to be resilient in the face of this.

          http://blog.stannard.net.au/2011/06/25/another-10-interesting-javascript-features/
          #5
          highlights this case, advocating the typeof/string compare

          I personally prefer using the (void 0) expression which, while non-obvious,
          avoids the string compare.


          [Non-text portions of this message have been removed]
        • Erik Eckhardt
          So, does this mean that using `undefined` directly is bad practice and this is what JSLint should warn about? Or is it better to warn about possible
          Message 4 of 9 , Jul 15 5:51 PM
          • 0 Attachment
            So, does this mean that using `undefined` directly is bad practice and this
            is what JSLint should warn about?

            Or is it better to warn about possible redefinitions of window.undefined
            (which I know could be difficult through static code analysis).

            Or should we grab our own `undefined` use that?

            var myscope = (function () {

            return {

            'undefined': [][0],

            somefunc: function (data) {

            if (data === myscope.undefined) {

            return;

            }

            // do something

            }

            };

            }());


            On Fri, Jul 15, 2011 at 4:52 PM, Joshua Bell <josh@...> wrote:

            > **
            >
            >
            > On Fri, Jul 15, 2011 at 3:44 PM, Erik Eckhardt <erik@...> wrote:
            >
            > > I just saw in a small jQuery plugin I was borrowing for my own use:
            > >
            > > function checkUndefined(a) {
            > > return typeof a === 'undefined';
            > > };
            > >
            > > if (checkUndefined(data)) {
            > > return false;
            > > }
            > >
            > > Not only does this seem like unneeded indirection, but is it even best to
            > > use the `typeof` operator here?
            > >
            >
            > In older browsers, the value of window.undefined can be modified; code may
            > wish to be resilient in the face of this.
            >
            >
            > http://blog.stannard.net.au/2011/06/25/another-10-interesting-javascript-features/
            > #5
            > highlights this case, advocating the typeof/string compare
            >
            > I personally prefer using the (void 0) expression which, while non-obvious,
            > avoids the string compare.
            >
            > [Non-text portions of this message have been removed]
            >
            >
            >


            [Non-text portions of this message have been removed]
          • Jordan
            Since undefined can be redefined, `typeof variable === undefined ` is the best practice, as far as I am aware. JSLint, in my opinion, should never allow usage
            Message 5 of 9 , Jul 15 10:40 PM
            • 0 Attachment
              Since undefined can be redefined, `typeof variable === 'undefined'` is the best practice, as far as I am aware. JSLint, in my opinion, should never allow usage of `undefined` on the global object.

              --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
              >
              > I just saw in a small jQuery plugin I was borrowing for my own use:
              >
              > function checkUndefined(a) {
              > return typeof a === 'undefined';
              > };
              >
              > if (checkUndefined(data)) {
              > return false;
              > }
              >
              > Not only does this seem like unneeded indirection, but is it even best to
              > use the `typeof` operator here?
              >
              > I thought that simply doing the following would be most clear:
              >
              > if (data === undefined) {
              > return false;
              > }
              >
              > Would alerting on this type of unnecessary type checking be useful?
              >
              > Note: I also appreciate JSLint suggesting String(variable) rather than
              > (variable + '').
              >
              >
              > [Non-text portions of this message have been removed]
              >
            • Rob Richardson
              Erik, The form I ve typically seen for this is something like this: (function (window, document, undefined) { ````// your logic here }(window,
              Message 6 of 9 , Jul 19 9:01 AM
              • 0 Attachment
                Erik,

                The form I've typically seen for this is something like this:

                (function (window, document, undefined) {
                ````// your logic here
                }(window, window.document));

                Note that the method is called with 2 arguments but has 3 parameters. Thus,
                by definition, the third argument is the correct value of undefined -- even
                if a careless or malicious user redefined window.undefined.

                In particular, jQuery does it this way, and Paul Irish spends a good deal of
                time discussing it at
                http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/
                starting about 1:30.

                Rob


                -----Original Message-----
                From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
                Behalf Of Erik Eckhardt
                Sent: Friday, July 15, 2011 5:51 PM
                To: jslint_com@yahoogroups.com
                Subject: Re: [jslint] Proposal for jslint functionality

                So, does this mean that using `undefined` directly is bad practice and this
                is what JSLint should warn about?

                Or is it better to warn about possible redefinitions of window.undefined
                (which I know could be difficult through static code analysis).

                Or should we grab our own `undefined` use that?

                var myscope = (function () {

                return {

                'undefined': [][0],

                somefunc: function (data) {

                if (data === myscope.undefined) {

                return;

                }

                // do something

                }

                };

                }());


                On Fri, Jul 15, 2011 at 4:52 PM, Joshua Bell <josh@...> wrote:

                > **
                >
                >
                > On Fri, Jul 15, 2011 at 3:44 PM, Erik Eckhardt <erik@...> wrote:
                >
                > > I just saw in a small jQuery plugin I was borrowing for my own use:
                > >
                > > function checkUndefined(a) {
                > > return typeof a === 'undefined';
                > > };
                > >
                > > if (checkUndefined(data)) {
                > > return false;
                > > }
                > >
                > > Not only does this seem like unneeded indirection, but is it even best
                to
                > > use the `typeof` operator here?
                > >
                >
                > In older browsers, the value of window.undefined can be modified; code may
                > wish to be resilient in the face of this.
                >
                >
                >
                http://blog.stannard.net.au/2011/06/25/another-10-interesting-javascript-fea
                tures/
                > #5
                > highlights this case, advocating the typeof/string compare
                >
                > I personally prefer using the (void 0) expression which, while
                non-obvious,
                > avoids the string compare.
                >
                > [Non-text portions of this message have been removed]
                >
                >
                >


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



                ------------------------------------

                Yahoo! Groups Links
              • Joshua Bell
                Again, let me promote the use of (void 0). if (value === (void 0)) { ... } In JavaScript, void is a unary operator (void EXPR) that always returns undefined.
                Message 7 of 9 , Jul 19 9:59 AM
                • 0 Attachment
                  Again, let me promote the use of (void 0).

                  if (value === (void 0)) { ... }

                  In JavaScript, void is a unary operator (void EXPR) that always returns
                  undefined. The expression following the operator is evaluated but ignored.
                  This does not depend on the browser environment (as the "undefined" property
                  of the global object does). Examples such as this use it:

                  https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter

                  In theory, JSLint permits a single numeric argument following void, warning
                  on any other usage. At least, that's how it used to behave, and that's how
                  the source reads (line 3530 or so). That said, it appears to be warning now
                  regardless of the argument. Recent bug?

                  On Tue, Jul 19, 2011 at 9:01 AM, Rob Richardson <erobrich@...>wrote:

                  > Erik,
                  >
                  > The form I've typically seen for this is something like this:
                  >
                  > (function (window, document, undefined) {
                  > ````// your logic here
                  > }(window, window.document));
                  >
                  > Note that the method is called with 2 arguments but has 3 parameters.
                  > Thus,
                  > by definition, the third argument is the correct value of undefined -- even
                  > if a careless or malicious user redefined window.undefined.
                  >
                  > In particular, jQuery does it this way, and Paul Irish spends a good deal
                  > of
                  > time discussing it at
                  > http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/
                  > starting about 1:30.
                  >
                  > Rob
                  >
                  >
                  > -----Original Message-----
                  > From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
                  > Behalf Of Erik Eckhardt
                  > Sent: Friday, July 15, 2011 5:51 PM
                  > To: jslint_com@yahoogroups.com
                  > Subject: Re: [jslint] Proposal for jslint functionality
                  >
                  > So, does this mean that using `undefined` directly is bad practice and this
                  > is what JSLint should warn about?
                  >
                  > Or is it better to warn about possible redefinitions of window.undefined
                  > (which I know could be difficult through static code analysis).
                  >
                  > Or should we grab our own `undefined` use that?
                  >
                  > var myscope = (function () {
                  >
                  > return {
                  >
                  > 'undefined': [][0],
                  >
                  > somefunc: function (data) {
                  >
                  > if (data === myscope.undefined) {
                  >
                  > return;
                  >
                  > }
                  >
                  > // do something
                  >
                  > }
                  >
                  > };
                  >
                  > }());
                  >
                  >
                  > On Fri, Jul 15, 2011 at 4:52 PM, Joshua Bell <josh@...> wrote:
                  >
                  > > **
                  > >
                  > >
                  > > On Fri, Jul 15, 2011 at 3:44 PM, Erik Eckhardt <erik@...>
                  > wrote:
                  > >
                  > > > I just saw in a small jQuery plugin I was borrowing for my own use:
                  > > >
                  > > > function checkUndefined(a) {
                  > > > return typeof a === 'undefined';
                  > > > };
                  > > >
                  > > > if (checkUndefined(data)) {
                  > > > return false;
                  > > > }
                  > > >
                  > > > Not only does this seem like unneeded indirection, but is it even best
                  > to
                  > > > use the `typeof` operator here?
                  > > >
                  > >
                  > > In older browsers, the value of window.undefined can be modified; code
                  > may
                  > > wish to be resilient in the face of this.
                  > >
                  > >
                  > >
                  >
                  > http://blog.stannard.net.au/2011/06/25/another-10-interesting-javascript-fea
                  > tures/
                  > > #5
                  > > highlights this case, advocating the typeof/string compare
                  > >
                  > > I personally prefer using the (void 0) expression which, while
                  > non-obvious,
                  > > avoids the string compare.
                  > >
                  > > [Non-text portions of this message have been removed]
                  > >
                  > >
                  > >
                  >
                  >
                  > [Non-text portions of this message have been removed]
                  >
                  >
                  >
                  > ------------------------------------
                  >
                  > Yahoo! Groups Links
                  >
                  >
                  >
                  >
                  >
                  > ------------------------------------
                  >
                  > Yahoo! Groups Links
                  >
                  >
                  >
                  >


                  [Non-text portions of this message have been removed]
                • Douglas Crockford
                  ... JSLint allows void 0 as a transitional convenience. But if the ES5 option is selected, it will produce a warning. In ES3, undefined is a global variable,
                  Message 8 of 9 , Jul 19 11:38 AM
                  • 0 Attachment
                    --- In jslint_com@yahoogroups.com, Joshua Bell <josh@...> wrote:
                    >
                    > Again, let me promote the use of (void 0).
                    >
                    > if (value === (void 0)) { ... }
                    >
                    > In JavaScript, void is a unary operator (void EXPR) that always returns
                    > undefined. The expression following the operator is evaluated but ignored.
                    > This does not depend on the browser environment (as the "undefined" property
                    > of the global object does).


                    JSLint allows void 0 as a transitional convenience. But if the ES5 option is selected, it will produce a warning.

                    In ES3, undefined is a global variable, so there is an obvious hazard because the value of this important constant can be changed. Despite the obvious hazard, I have never heard of a single instance where this was a real problem.

                    In ES5, the hazard is corrected by making undefined a const, as it always should have been.
                  Your message has been successfully submitted and would be delivered to recipients shortly.