Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getOwnPropertyNames does not fetch methods of Date instance

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading