0

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?

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
<p id="demo"></p>

Is there any way to see age as a key inside the object myCar ? -- except the trivial one of putting some

    this.age = function() {} 

inside the constructor.

When invoking the method as myCar.age() it works (while it can't be seen as an own object key or method, so the age key is invisible for accessing it through the usual for loop from above, or through Object.keys(), Object.values() and Object.entries() as well.

I am not sure if this can be also related to the fact that Object.keys() creates an array only from user-defined objects (not from JS built-in ones) -- for example doing Object.keys(Math) will just return an empty array instead of that big list seen when doing console.dir(Math):

    console.log(Object.keys(Math)); //   []

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 this.age = something() 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.

More, I can't just use something as

    if('age' in myCar){
        alert("yes, i have that method");
    }

as suggested by an answer here because I don't know beforehand the name of age method (or the name of some other methods dynamically served from that script during the time).

Barmar
  • 741,623
  • 53
  • 500
  • 612
Eve
  • 357
  • 3
  • 12
  • 1
    Methods are in the class prototype, not the object itself. – Barmar Nov 17 '21 at 22:28
  • If you don't know the name of the method you care about, how will you use any solution to this problem? – Barmar Nov 17 '21 at 22:33
  • See this: https://stackoverflow.com/questions/30881632/es6-iterate-over-class-methods – Steve Nov 17 '21 at 22:34
  • @Steve I checked the resource, thank you, but it didn't answer my question in full. The right answer might be actually `console.log(Object.getOwnPropertyNames(myCar.constructor.prototype))` because I needed a way to see the methods inherited by the object not the class itself. Therefore instead of Car.prototype I will use myCar.constructor.prototype inside the getOwnPropertyNames. I'd actually make this into an acceptable answer to my question. Also, maybe explaining anything (or linking some answer) about built in vs own vs prototype inherited methods would probably help much more any reader – Eve Nov 17 '21 at 23:01
  • @Barmar I initially know the methods however in a real case the external code changed out of my control (replacing outdated methods, adding new ones, etc). It seems though I missed the prototype things until now. TLDR Somehow I reached to the result using the similar question linked so even closing my question was helpful for me. Thank you – Eve Nov 17 '21 at 23:11
  • But if the external code changes, how will you know what to look for? I still don't really understand how you will use this in the general case. – Barmar Nov 17 '21 at 23:17
  • @Barmar Instead to inspect manually the external code from time to time and update my own script, I just needed a filter to validate how many methods are 'invisible' after I check them initially. I had an alert in the admin page however I am going to put my own methods instead in the code. Sometimes the outside business logic changes. Case in hand, in a last year covid reporting based project the outside intl devs inserted new data aggregation systems (after vaccines were introduced) so manually checking for 234 territories randomly changing reports was not a good choice for me – Eve Nov 17 '21 at 23:38

0 Answers0