At this thread it is suggested that Object.getOwnPropertyNames should return all properties of an object including functions. But when I do the following;
let date1 = new Date();
Object.getOwnPropertyNames(date1);
this returns 0 results. But date1 has methods like date1.toISOString().
How can I fetch all those methods?
>Solution :
getOwnPropertyNames returns – as it sounds – own property names. .toISOString is not a method on the Date itself – it’s a method on the prototype.
For the same reason, the below does not log method.
class C {
method() {}
}
const c = new C();
console.log(Object.getOwnPropertyNames(c));
Rather, they’re properties on the internal prototype of the object – Date.prototype. So, call it on the prototype:
const date1 = new Date();
console.log(Object.getOwnPropertyNames(Date.prototype));