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

void operator and Object-as-function

Expand Messages
  • Joshua Bell
    The indexOf polyfill (as the kids would call it today) sample shown on https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
    Message 1 of 1 , Feb 7, 2011
    • 0 Attachment
      The "indexOf polyfill" (as the kids would call it today) sample shown on
      https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
      uses
      two parts of JavaScript for which JSLint gives unhelpful errors:

      * The void operator (coerce value to undefined)
      * The Object constructor called as a function (boxes primitives)

      These are not commonly used, but IMHO JSLint should either (1) parse these
      forms but reject them, with explicit errors about why they are "bad parts"
      or (2) accept them. The current behavior gives erroneous output which is not
      helpful to users who may be validating such samples.

      Examples:

      var undef = void 0;
      >>> Error: Problem at line 1 character 18: Expected ';' and instead saw '0'.

      var s = String(123);
      var n = Number("123");
      var o = Object("123");
      >>> Error: Problem at line 3 character 9: Missing 'new'.

      Justification for including them:

      * In earlier versions of ECMAScript (and implemented in many current
      browsers), the Global object's "undefined" property can be successfully
      assigned to, e.g. window.undefined = "foo". (ES5 defines this property with
      [[Writable]]: false)

      * The Object constructor called as a function parallels the String and
      Number constructors-as-functions in that it boxes only if necessary. For
      example, if it is desired to set a property on a value but the value may be
      a string, this form can be used: o = Object(value); o.prop = "decoration";
      manual boxing in this form would require either using the "new String"/"new
      Number" constructors which are rejected by JSLint, or creating objects with
      toString/valueOf properties to emulate the boxed value.

      Justification for rejecting them:

      * The void operator is very obscure, and its use makes code difficult to
      understand. Alternate forms include relying on the global "undefined"
      property (i.e. in browsers, window.undefined), simply using an unassigned
      variable (e.g. var undef1;) or returning it from a function (e.g. var undef2
      = (function() { return; }());). Further, the actual cases for generating
      undefined values are rare; using typeof tests are clearer, i.e.: typeof foo
      === 'undefined'

      * The use cases for Object() are rare, and explicit implementation of a
      boxing mechanism in user code (while potentially fragile) will be far more
      readable.

      Again, I'm not advocating for approving the usage of these, merely
      suggesting that JSLint could detect and explicitly reject these uses.

      -- Josh


      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.