Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What it's doing is seeing if that property name exists in a blank object. So for instance, it will filter out <code>toString</code> (on implementations that aren't buggy about <code>toString</code> and <code>in</code>; IE is).</p> <p>It may be an attempt to work around people adding things to the <code>Object.prototype</code> (which is a very, very bad idea). For instance:</p> <pre><code>Object.prototype.foo = "bar"; // &lt;== VERY BAD IDEA alert({}.foo); // alerts "bar" function Thingy(a, b) { this.a = a; this.b = b; } Thingy.prototype.x = 42; var t = new Thingy(1, 2); </code></pre> <p>If I want to copy <code>t</code>, including its inherited <code>x</code> property from <code>Thingy</code> but <strong>not</strong> including the <code>foo</code> property it inherits from <code>Object</code>, I could use that <code>if (name in {})</code> to skip those.</p> <p>Mind you, it's not a very smart way to do that, as it will fail if I do this:</p> <pre><code>var t = new Thingy(1, 2); t.foo = "charlie"; </code></pre> <p>Now <code>t</code> has its <em>own</em> <code>foo</code>, which presumably I would have wanted to copy.</p> <p>A more thorough check would be:</p> <pre><code>dest = {}; blank = {}; // No need to recreate it on every iteration for (name in src) { if (src.hasOwnProperty(name) || !(name in blank)) { dest[name] = src[name]; } } </code></pre> <p>That will only filter out ones that are inherited, or at least might be inherited, from <code>Object</code>. But even <em>that's</em> flawed; what if it inherits a value for the property, but not the value from <code>Object</code> (e.g., something in the middle&nbsp;&mdash; <code>Thingy</code> perhaps&nbsp;&mdash; has overridden <code>Object</code>'s default)?</p> <p>So you may want to refactor this code you've inherited, probably in terms of using <code>hasOwnProperty</code> and making sure that nothing anywhere in your application puts things on <code>Object.prototype</code>.</p>
    singulars
    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.
    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