Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 422
Games Brainiac Avatar asked Jun 29 '13 14:06

Games Brainiac


People also ask

What does dir () mean in python?

The dir() function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are default for all object.

Is there a python equivalent for JavaScript?

Python's "object-based" subset is roughly equivalent to JavaScript. Like JavaScript (and unlike Java), Python supports a programming style that uses simple functions and variables without engaging in class definitions.

What is console dir in JavaScript?

dir() The method console. dir() displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.

What is __ DIR __ in python?

Python dir() function returns the list of names in the current local scope. If the object on which method is called has a method named __dir__(), this method will be called and must return the list of attributes. It takes a single object type argument. The signature of the function is given below.


2 Answers

You could use Object.keys(), e.g.:

> 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"]
like image 120
NPE Avatar answered Sep 30 '22 19:09

NPE


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.

like image 34
gitaarik Avatar answered Sep 30 '22 20:09

gitaarik