title: hasOwnProperty returns true, when checked against parent object properties
body: <p>My JavaScript code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>console.clear();
  
  function BaseClass(nname) {
    var name = nname;
  
    this.bc_PublicProperty = "DefaultValue_BaseClass";
  
    this.bc_getName = function GetName() {
      return name;
    };
  
    this.bc_setName = function SetName(nname) {
      name = nname;
    };
  }
  
  function SubClass(nname) {
    BaseClass.call(this, nname);
  
    this.sc_PublicProperty = "DefaultValue_SubClass";
  
    this.sc_getName = function GetName() {
      return name;
    };
  
    this.sc_setName = function SetName(nname) {
      name = nname;
    };
  }
  
  SubClass.prototype = Object.create(BaseClass.prototype);
  SubClass.prototype.constructor = SubClass;
  
  var bc = new BaseClass("Anton");
  var sc = new SubClass("Bernd");
  
  console.log("hasOwnProperty check on subclass object 'sc' returns true for Method 'bc_getName'");
  
  for (var pro in sc) {
    console.log("Is " + pro + " own property of subclass:  --&gt; " + Object.hasOwnProperty.call(sc, pro));
  
  }</code></pre>
  </div>
  </div>
  </p>
  <p>I have two objects (BaseClass and SubClass). One inherits from the other using the constructor pattern, as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript" rel="nofollow noreferrer">explained on MDN</a>.</p>
  <p>Now, when I iterate over all properties of the subclass object, they all return true for hasOwnProperty, even for the parent methods/properties, except constructor.</p>
  <p>Does it mean, that it breaks when using the constructor pattern?</p>
  <p>When I put public properties and methods in the constructor function, whether in BaseClass or SubClass, they will always be &quot;marked&quot; as own properties/methods. When I instead attach them to the respective prototype, the hasOwnProperty will output &quot;false&quot; for them.</p>
  <p>Whether you put the public methods/properties to the prototype or to the constructor function itself, they will all be available for use in the subClass (--&gt; SubClass2, --&gt; SubClass3).</p>
  <p>The only thing, that I can think of right now, why you should attach them to the prototype object, is because of efficiency reasons, as explained <a href="https://developers.google.com/speed/articles/optimizing-javascript#Initializing" rel="nofollow noreferrer">here</a>, &quot;Defining class methods&quot;-section. In order not to add closures for every constructed instance.</p>
  <p>Value Types should be declared on the prototype, but not for instance variables whose initial values are dependent on arguments to the constructor, or some other state at time of construction.
  You can override both properties/functions irrespective of their place of declaration.</p>
  <p>Also setting up getters and setters on the prototype, for instance to set or get a private variable, makes no sense, as the private variable has to be public, in order to be accessible by the getters and setters attached to the prototype.</p>
  <p>Therefore it makes no sense to use getters and setters. You can access the public variable directly.</p>
  <p>I have to adjust my question a bit now:</p>
  <p>When do I need hasOwnProperty, if actually public props/functions should be declared on the prototype, which in turn will all output Object.hasOwnProperty(obj,&quot;prop/func&quot;) --&gt; false. Give me a use case, when it makes sense, to use hasOwnProperty.</p>
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>console.clear();
  
  var l = function(v) {
    console.log(v);
  };
  
  function BaseClass(nname) {
  
    this.bc_nameProp = nname;
    this.bc_ownFunc = function() {
      l("bc_ownFunc of BaseClass called");
    };
  
    this.bc_getName = function GetName() {
      return this.bc_nameProp;
    };
  }
  
  BaseClass.prototype.bc_getName = function GetName() {
    return this.bc_nameProp;
  };
  
  BaseClass.prototype.bc_PublicProperty = "DefaultValue_BaseClass";
  
  BaseClass.prototype.bc_setName = function SetName(nname) {
    bc_nameProp = nname;
  };
  
  function SubClass(nname) {
  
    BaseClass.call(this, nname);
  
    this.sc_setName = function SetName(nname) {
      bc_nameProp = nname;
    };
  
    this.bc_getName = function GetName() {
      return "xyz";
    };
  
  }
  
  SubClass.prototype = Object.create(BaseClass.prototype);
  SubClass.prototype.constructor = SubClass;
  
  SubClass.prototype.sc_getName = function GetName() {
    return this.bc_nameProp;
  };
  SubClass.prototype.sc_PublicProperty = "DefaultValue_SubClass";
  
  var bc = new BaseClass("Anton");
  var sc = new SubClass("Bernd");
  
  l("-----  iterating over BaseClass properties  ------");
  l("");
  for (var pro in bc) {
    l("Is " + pro + " own property of BaseClass:  --&gt; " + Object.hasOwnProperty.call(bc, pro));
  
  }
  l("");
  l("-----  iterating over SubClass properties  ------");
  l("");
  for (var p in sc) {
    l("Is " + p + " own property of subclass:  --&gt; " + Object.hasOwnProperty.call(sc, p));
  
  }
  
  l(bc.bc_getName());
  l(sc.bc_getName());
  l(sc.sc_getName());</code></pre>
  </div>
  </div>
  </p>
  <hr/>
  <h3>Solution</h3>
  <p>When I asked this question, I was of the opinion, that when using classical inheritance in JavaScript, I can differentiate which properties/functions of my subclass directly belong to it, using hasOwnProperty. Which is not possible, as all the properties/functions of the parent prototype are copied to the prototype of the SubClass:</p>
  <pre><code>SubClass.prototype = Object.create(BaseClass.prototype);
  </code></pre>
  <p>All properties/functions attached to prototypes return &quot;false&quot;, when using hasOwnProperty.</p>
  <p>And if you have public properties/functions declared in the constructor functions of BaseClass and SubClass, all these properties return &quot;true&quot;, when calling hasOwnProperty for these properties on the subclass.</p>
  <p>These are copied to the SubClass using this statement:</p>
  <pre><code>BaseClass.call(this, nname);
  </code></pre>
  <p>So using hasOwnProperty while iterating over all properties of an obj of type SubClass, will output false for all properties/functions declared on prototype level and true for all properties/functions declared on constructor function level.</p>
  <p>Now, I understand why using hasOwnProperty in this use case makes no sense.</p>