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

Re: [jslint] Proposal for jslint functionality

Expand Messages
  • 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 1 of 9 , Jul 19, 2011
    • 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 2 of 9 , Jul 19, 2011
      • 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.