简体   繁体   中英

Date's prototype methods are not enumerable. How to obtain a list of them to iterate?

I want to iterate over the methods of a javascript Date object. The first attempt did not work:

var date = new Date();
for (var key in date) {
    console.log(key);
}

I've tried a few related things -- Object.keys , for example. So far I have not found a way to do it.

From MDN :

The Object.getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except properties that using Symbol) found directly upon a given object.

So Object.getOwnPropertyNames will produce the list I was looking for when called directly on the prototype object:

Object.getOwnPropertyNames(Date.prototype)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM