Listing the available properties / functions for Date in JavaScript

后端 未结 2 1352
失恋的感觉
失恋的感觉 2021-01-29 05:07

I am trying to list the properties / functions for Date in JavaScript using the below code:

var mydate=new Date();
for(var i in mydate){
   console.log(i);
}


        
相关标签:
2条回答
  • 2021-01-29 05:48

    Properties of date objects (or rather those that they inherit) are non-enumerable. Using Object.getOwnPropertyNames does reaveal them, so you can try this instead:

    for (var o=new Date(); o!=null; o=Object.getPrototypeOf(o)) {
        console.log("on ["+o+"] itself:");
        Object.getOwnPropertyNames(o).forEach(function(p) {
            console.log(p)
        });
    }
    

    Alternatively, for checking the properties of native objects, you might just fire up their Docs - either in the spec or at MDN. Also, simply console.logging them usually produces an expandable view on an object that also includes the prototypes and their properties.

    0 讨论(0)
  • 2021-01-29 05:53

    Many built-in objects have their methods set to not be enumerable, and therefore won't show up in a for..in loop.

    You should instead look up some documentation, and then you can test if your browser supports certain things like:

    if( 'now' in Date)
    
    0 讨论(0)
提交回复
热议问题