Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ignore the error. The "in" operator is clearly defined in <a href="http://ecma-international.org/ecma-262/5.1/" rel="noreferrer">ECMA 262-5.1 / June 2011</a> sec-11.8.7</p> <p>There appear to be only three outcomes for <code>'someProperty' in anObject</code> which is one of: <code>true</code>, <code>false</code>, or a <code>TypeError</code> exception is thrown. When it evaluates to <code>true</code>, then there is definitely <code>'someProperty' in anObject</code>. When it evaluates to <code>false</code> there is definitely not <code>'someProperty' in anObject</code>. When a <code>TypeError</code> is thrown there is definitely not <code>'someProperty' in anObject</code> because <code>anObject</code> is either null or it isn't an object at all. This all seems very clear to me. When I want to know if an object has a property and, I don't care if that property is the object's own property or being inherited and, I don't care what the value of the property is, then I simply look for <code>'someProperty' in anObject</code>.</p> <h3> Warning </h3> <p><i>Caution: some would have you check for <code>anObject.someProperty !== undefined</code> but that isn't really checking whether or not the object has the property. What it's doing is checking whether the object has the property <b>AND</b> that the value of that property is <b>NOT</b> undefined. Some would have you check for <code>anObject.hasOwnProperty('someProperty');</code> but that will only tell you if the object has that property <b>AND</b> has <b>NOT</b> inherited it somehow. Don't believe me? Try the following:</i></p> <pre><code>console.log(document.body.tagName); // BODY console.log(document.body.hasOwnProperty('tagName')); // false, it's inherited console.log('tagName' in document.body); // true, it does have the property document.body.wobbles = undefined; // new property added to document.body console.log('wobbles' in document.body); // true, it does have the property console.log(document.body.wobbles !== undefined); // false, the value really IS undefined </code></pre> <p>I've written an article about the "in" operator that goes into more detail. If you want to read it it's at: <a href="http://matthewkastor.blogspot.com/2012/09/Unexpected--in---Compare-with-undefined--or-use-the-hasOwnProperty-method-instead.html" rel="noreferrer"><a href="http://matthewkastor.blogspot.com/2012/09/Unexpected--in---Compare-with-undefined--or-use-the-hasOwnProperty-method-instead.html" rel="noreferrer">http://matthewkastor.blogspot.com/2012/09/Unexpected--in---Compare-with-undefined--or-use-the-hasOwnProperty-method-instead.html</a></a> The bottom line is that you should just ignore this error and wrap things up in a try catch block if the object might be null or not an object.</p> <pre><code>function hasProperty(prop, obj) { try { return prop in obj; } catch(e) { return e; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload