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

Re: Object constructor used as a function

Expand Messages
  • Douglas Crockford
    ... No. Object(o) was not created for that purpose. That code is depending on an edge case. Avoid the edges. Use this instead: typeof o === object && !o
    Message 1 of 4 , Aug 25 8:10 AM
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Chris" <Nielsen.Chris@...> wrote:
      > JavaScript 1.8.5 introduces a new method of the Object constructor called Object.keys, which produces an array of the enumerable properties of a given object, excluding any properties from the prototype chain.
      >
      > The MDN documentation for this suggests a snippet that adds this method to browsers that do not yet support it:
      >
      > if(!Object.keys) Object.keys = function(o){
      >  if (o !== Object(o))
      >       throw new TypeError('Object.keys called on non-object');
      >  var ret=[],p;
      >  for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
      >  return ret;
      > }
      >
      > https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
      >
      > On line two, they have used an interesting construct: if (o !== Object(o)). This returns true for objects but false for strings, numbers, and booleans.
      >
      > JSLint reports this as an error, stating that the object literal notation should be used instead. Clearly, the object literal notation cannot be used in this case.
      >
      > JSLint does not complain about similar constructors being used in this manner. For instance, it is permitted to say (o !== String(o)) or (o !== Number(o)).
      >
      > Should this usage of (o !== Object(o)) also be allowed?

      No. Object(o) was not created for that purpose. That code is depending on an edge case. Avoid the edges. Use this instead:

      typeof o === 'object' && !o

      There is a chance that a future edition may correct the typeof null problem, in which case

      typeof o === 'object'

      will be sufficient.
    • Jakob Kruse
      var o = {}; ... typeof o === object && !o ... Doesn t seem like a very good object detection test? What should it have said? /Jakob _____ From: Douglas
      Message 2 of 4 , Aug 25 8:30 AM
      • 0 Attachment
        var o = {};
        >> undefined


        typeof o === 'object' && !o
        >> false


        Doesn't seem like a very good object detection test? What should it have said?


        /Jakob



        _____

        From: Douglas Crockford [mailto:douglas@...]
        To: jslint_com@yahoogroups.com
        Sent: Thu, 25 Aug 2011 17:10:02 +0200
        Subject: [jslint] Re: Object constructor used as a function






        --- In jslint_com@yahoogroups.com, "Chris" <Nielsen.Chris@...> wrote:
        > JavaScript 1.8.5 introduces a new method of the Object constructor called Object.keys, which produces an array of the enumerable properties of a given object, excluding any properties from the prototype chain.
        >
        > The MDN documentation for this suggests a snippet that adds this method to browsers that do not yet support it:
        >
        > if(!Object.keys) Object.keys = function(o){
        > if (o !== Object(o))
        > throw new TypeError('Object.keys called on non-object');
        > var ret=[],p;
        > for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
        > return ret;
        > }
        >
        > https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
        >
        > On line two, they have used an interesting construct: if (o !== Object(o)). This returns true for objects but false for strings, numbers, and booleans.
        >
        > JSLint reports this as an error, stating that the object literal notation should be used instead. Clearly, the object literal notation cannot be used in this case.
        >
        > JSLint does not complain about similar constructors being used in this manner. For instance, it is permitted to say (o !== String(o)) or (o !== Number(o)).
        >
        > Should this usage of (o !== Object(o)) also be allowed?

        No. Object(o) was not created for that purpose. That code is depending on an edge case. Avoid the edges. Use this instead:

        typeof o === 'object' && !o

        There is a chance that a future edition may correct the typeof null problem, in which case

        typeof o === 'object'

        will be sufficient.




        [Non-text portions of this message have been removed]
      • Ben White
        I think he meant to type this for object detection. typeof o === object && !!o Ben ... said? ... called Object.keys, which produces an array of the
        Message 3 of 4 , Aug 25 8:36 AM
        • 0 Attachment
          I think he meant to type this for object detection.

          typeof o === 'object' && !!o

          Ben
          On Aug 25, 2011 11:31 AM, "Jakob Kruse" <kruse@...> wrote:
          > var o = {};
          >>> undefined
          >
          >
          > typeof o === 'object' && !o
          >>> false
          >
          >
          > Doesn't seem like a very good object detection test? What should it have
          said?
          >
          >
          > /Jakob
          >
          >
          >
          > _____
          >
          > From: Douglas Crockford [mailto:douglas@...]
          > To: jslint_com@yahoogroups.com
          > Sent: Thu, 25 Aug 2011 17:10:02 +0200
          > Subject: [jslint] Re: Object constructor used as a function
          >
          >
          >
          >
          >
          >
          > --- In jslint_com@yahoogroups.com, "Chris" <Nielsen.Chris@...> wrote:
          > > JavaScript 1.8.5 introduces a new method of the Object constructor
          called Object.keys, which produces an array of the enumerable properties of
          a given object, excluding any properties from the prototype chain.
          > >
          > > The MDN documentation for this suggests a snippet that adds this method
          to browsers that do not yet support it:
          > >
          > > if(!Object.keys) Object.keys = function(o){
          > > if (o !== Object(o))
          > > throw new TypeError('Object.keys called on non-object');
          > > var ret=[],p;
          > > for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
          > > return ret;
          > > }
          > >
          > >
          https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
          > >
          > > On line two, they have used an interesting construct: if (o !==
          Object(o)). This returns true for objects but false for strings, numbers,
          and booleans.
          > >
          > > JSLint reports this as an error, stating that the object literal
          notation should be used instead. Clearly, the object literal notation cannot
          be used in this case.
          > >
          > > JSLint does not complain about similar constructors being used in this
          manner. For instance, it is permitted to say (o !== String(o)) or (o !==
          Number(o)).
          > >
          > > Should this usage of (o !== Object(o)) also be allowed?
          >
          > No. Object(o) was not created for that purpose. That code is depending on
          an edge case. Avoid the edges. Use this instead:
          >
          > typeof o === 'object' && !o
          >
          > There is a chance that a future edition may correct the typeof null
          problem, in which case
          >
          > typeof o === 'object'
          >
          > will be sufficient.
          >
          >
          >
          >
          > [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.