Equivalent of Python's dir in Javascript

后端 未结 6 1572
孤城傲影
孤城傲影 2020-12-03 04:06

when I write Python code from the interpreter I can type dir() to have a list of names defined in the current scope. How can achieve to have the same informatio

相关标签:
6条回答
  • 2020-12-03 04:44

    This may work for you, if you need a simple solution:

    function dir(object) {
        stuff = [];
        for (s in object) {
            stuff.push(s);
        }
        stuff.sort();
        return stuff;
    }
    
    0 讨论(0)
  • 2020-12-03 04:45

    well you can see object contains like its own properties only : By it can work in any console not only google chrome web browser look for the img enter image description here console.dir(obj); here link: https://developers.google.com/web/tools/chrome-devtools/console/console-reference

    0 讨论(0)
  • 2020-12-03 04:47

    There is "keys" method in Object, for example:

    Object.keys(object)
    

    But this return object's own properties and methods only.
    To list all properties and methods of an object I know 2 possibilities:
    1. console.dir(object) method in firebug console for Firefox and
    2. dir(object) method in Google Chrome development tools.

    0 讨论(0)
  • 2020-12-03 04:49

    The global variables are kept in an easily accessible object (window) and so you can inspect/iterate over them easily. (Using something like the functions suggested by Glenjamin)

    On the other hand, I don't know of any way to inspect local variables defined in functions or closures - if this is possible I'd at least guess it would be highly browser/console specific.

    0 讨论(0)
  • 2020-12-03 05:02

    There are a couple of functions which do just this in the code for ChatZilla, you'll have to check the licence properly to see if you can just rip them out and use them wherever.

    The relevant functions can be found at http://hg.mozilla.org/chatzilla/file/59b46c0bf716/js/lib/utils.js#l136 dumpObject and dumpObjectTree

    0 讨论(0)
  • The Google Chrome developer tools console has a predefined dir: https://developers.google.com/chrome-developer-tools/docs/console

    Firebug has console.dir: http://getfirebug.com/logging

    0 讨论(0)
提交回复
热议问题