简体   繁体   中英

Python dir() equivalent in JavaScript?

What I loved about Python was that if you wanted to know something about a particular module, you could just go something like this:

dir(django.auth.models)

and it would give you all the things inside of models , is there something similar to this in JavaScript?

You could use Object.keys() , eg:

> Object.keys(window)
["top", "window", "location", "external", "chrome", "Intl", "v8Intl", "document", "$", "jQuery", "MSIsPlayback", "i", "prepareEditor", "StackExchange", "scriptSrc", "careers_adurl", "careers_cssurl", "careers_leaderboardcssurl", "careers_companycssurl", "careers_adselector", "_gaq", "_qevents", "jQuery171008060155878774822", "__qc", "quantserve", "uh", "_gat", "gaGlobal", "gauth", "genuwine", "moveScroller", "styleCode", "sanitizeAndSplitTags", "initTagRenderer", "showFadingHelpText", "initFadingHelpText", "profileLink", "EventEmitter", "votesCast", "tagRendererRaw", "tagRenderer", "ytCinema", "IN_GLOBAL_SCOPE", "prettyPrintOne", "prettyPrint", "PR_SHOULD_USE_CONTINUATION", "PR", "Markdown", "apiCallbacks"]

If you do console.log(variable) in javascript, you'll see information about that variable in your browser's debugging console. If the variable is an object for example, you'll see it's attributes.

I don´t know if doesn´t have this option in 2013, but today we have:

console.dir(variable)

ps: This answer is for those fall here after a googling 'python dir in javascript' (like me! by the way).

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