title: Object created with a class is not showing its own method
body: <p>I have the code below, where I cannot access a method (to list its name) unless I call the function itself. The method name isn't enumerated in the keys of the object, so how could I else display that method of the object?</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class Car {
    constructor(carname, caryear) {
      this.name = carname;
      this.year = caryear;
    }
    age() {
      let date = new Date();
      return date.getFullYear() - this.year;
    }
  }
  
  let myCar = new Car("Ford", 2017);
  document.getElementById("demo").innerHTML =
    "My car is " + myCar.age() + " years old."; // 'My car is 4 years old.'  --- now in 2021
  
  alert(myCar.age); // it displays the function content
  
  for (key in myCar) { // it only shows   `name` and `year`  but not the `age`
    alert(key);
  }
  
  alert(Object.keys(myCar)); // it shows the aray   name, year</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p id="demo"&gt;&lt;/p&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Is there any way to see <em><strong>age</strong></em> as a key inside the object <em><strong>myCar</strong></em> ? -- except the trivial one of putting some</p>
  <pre><code>    this.age = function() {} 
  </code></pre>
  <p>inside the constructor.</p>
  <p>When invoking the method as <em>myCar.age()</em> it works (while it can't be seen as an own object key or method, so the <em>age</em> key is invisible for accessing it through the usual <em>for loop</em> from above, or through <em>Object.keys()</em>, <em>Object.values()</em> and <em>Object.entries()</em> as well.</p>
  <p>I am not sure if this can be also related to the fact that <em>Object.keys()</em> creates an array only from user-defined objects (not from JS built-in ones) -- for example doing <em>Object.keys(<strong>Math</strong>)</em> will just return an empty array instead of that big list seen when doing <em>console.dir(<strong>Math</strong>)</em>:</p>
  <pre><code>    console.log(Object.keys(Math)); //   []
  </code></pre>
  <p>The use case is when my code consumes some script from an external resource (the first script content above) where I cannot change the class definition (therefore using <em><code>this.age = something()</code></em> probably won't be a choice) so I would need to check the methods and validate what is ok to run over my own created objects, etc.</p>
  <p>More, I can't just use something as</p>
  <pre><code>    if('age' in myCar){
          alert(&quot;yes, i have that method&quot;);
      }
  </code></pre>
  <p>as suggested by an answer <a href="https://stackoverflow.com/questions/11040472/how-to-check-if-object-property-exists-with-a-variable-holding-the-property-name">here</a> because I don't know beforehand the name of <code>age</code> method (or the name of some other methods dynamically served from that script during the time).</p>