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

Converting non-objects into objects

Expand Messages
  • pauanyu
    I frequently find that I must convert non-objects into objects. Consider, for example, a function that expects one of it s arguments to be an object. If you
    Message 1 of 3 , Feb 15, 2010
    • 0 Attachment
      I frequently find that I must convert non-objects into objects. Consider, for example, a function that expects one of it's arguments to be an object. If you try to set a property on the expected object, but it turns out it's not an object, poof! You get an error.

      You can avoid this by doing a simple truthy check:

      if (object) {}


      This has problems if "object" is "true", however. It will pass the truthy test, but is still not an object.

      You can also use "instanceof":

      if (object instanceof Object) {}


      This is better, and works as expected. Let's suppose, however, you not only want to check if it really is an object, but (in addition), make it an object if it isn't. One use of this is to guarantee that no matter what the user specifies, the function will always return an object.

      You can do that like this:

      if (!(object instanceof Object)) {
      object = {};
      }


      Ick. That's not *that* bad, but it's still 3 lines of smelly code. Nonetheless, I've had times where I needed to use the above construct. Until I found a better alternative:

      object = Object(object);


      If the variable "object" is an object, it is simply returned, with all it's existing properties intact. If it is not an object, it is converted into one. This effectively does the same thing as the "if" block, while being much smaller, more readable, and potentially faster.

      JSLint, however, complains that it is missing the "new" prefix, so I dutifully add it in:

      object = new Object(object);


      Okay, it's not as semantically clear (in my opinion), but it's what JSLint wants. But, now it complains (rightfully so), that I should use the {} syntax to create an object.

      The problem is, I'm using this construct to ensure that the variable "object" is always an object, and doesn't overwrite existing properties. I can't use the {} syntax to do that.

      I'm curious what your thoughts on the matter are. Should the "object = Object(object)" syntax be allowed? In my opinion, since it is converting the variable into an object (rather than creating a new object), using the "new" prefix doesn't make sense. And using an "if" block is ugly and inefficient.

      I would love to be able to do this, without JSLint complaining:

      object = Object(object);
    • Douglas Crockford
      ... That s not going to happen. The wrapping of primitive values is one of the bad parts.
      Message 2 of 3 , Feb 15, 2010
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
        >
        > I frequently find that I must convert non-objects into objects. Consider, for example, a function that expects one of it's arguments to be an object. If you try to set a property on the expected object, but it turns out it's not an object, poof! You get an error.
        ...
        > I would love to be able to do this, without JSLint complaining:
        >
        > object = Object(object);


        That's not going to happen. The wrapping of primitive values is one of the bad parts.
      • pauanyu
        ... Forgive my ignorance, but what problems are caused by the construct I proposed?
        Message 3 of 3 , Feb 15, 2010
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
          >
          > That's not going to happen. The wrapping of primitive values is one of the bad parts.
          >

          Forgive my ignorance, but what problems are caused by the construct I proposed?
        Your message has been successfully submitted and would be delivered to recipients shortly.