如何查找 Javascript 对象中隐藏的属性/方法?

发布于 2024-09-03 12:02:45 字数 1073 浏览 0 评论 0原文

我想自动确定所有属性(包括 隐藏的)在给定的 Javascript 对象中,通过对此的概括 函数:

function keys(obj) {
    var ll = [];
    for(var pp in obj) {
        ll.push(pp);
    }
    return ll;
}

这适用于用户定义的对象,但对许多内置函数失败:

repl> keys({"a":10,"b":2});  // ["a","b"]
repl> keys(Math) // returns nothing!

基本上,我想编写Python的dir()和help()的等效项,它们在探索新对象时非常有用。

我的理解是,只有内置对象具有隐藏属性(用户代码 显然在 HTML5 之前无法设置“可枚举”属性),因此一种可能性是简单地将 Math、String 等属性硬编码为 dir() 等效项(使用诸如 < a href="http://www.w3schools.com/jsref" rel="nofollow noreferrer">此处)。但还有更好的办法吗?

编辑:好的,到目前为止我看到的最好的答案是此线程。您无法使用自己的 JS 代码轻松做到这一点,但下一个最好的办法是使用 Chrome 开发人员工具中的 console.dir(Chrome -> View -> Developer -> Developer Tools)。运行 console.dir(Math) 并单击三角形向下钻取以列出所有方法。这对于大多数交互式/发现工作来说已经足够了(您实际上不需要在运行时执行此操作)。

I would like to automatically determine all of the properties (including the
hidden ones) in a given Javascript object, via a generalization of this
function:

function keys(obj) {
    var ll = [];
    for(var pp in obj) {
        ll.push(pp);
    }
    return ll;
}

This works for user defined objects but fails for many builtins:

repl> keys({"a":10,"b":2});  // ["a","b"]
repl> keys(Math) // returns nothing!

Basically, I'd like to write equivalents of Python's dir() and help(), which are really useful in exploring new objects.

My understanding is that only the builtin objects have hidden properties (user code evidently can't set the "enumerable" property till HTML5), so one possibility is to simply hardcode the properties of Math, String, etc. into a dir() equivalent (using the lists such as those here). But is there a better way?

EDIT: Ok, the best answer I've seen so far is on this thread. You can't easily do this with your own JS code, but the next best thing is to use console.dir in Chrome's Developer Tools (Chrome -> View -> Developer -> Developer Tools). Run console.dir(Math) and click the triangular drill down to list all methods. That's good enough for most interactive/discovery work (you don't really need to do this at runtime).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甚是思念 2024-09-10 12:02:46

Object.getOwnPropertyNames 不会返回“隐藏的属性”。
Object.getOwnPropertyNames 返回非继承属性的名称。

Object.getOwnPropertyNames won't return "the hidden ones".
Object.getOwnPropertyNames returns the names of non-inherited properties.

梦里寻她 2024-09-10 12:02:46

This is explained in a previous answer. Basically, the spec explicitly requires (using DontEnum) that these objects aren't enumerable.

海拔太高太耀眼 2024-09-10 12:02:46

这在 firebug 中可以用来查找对象方法。

<块引用>
<块引用>

Object.getOwnPropertyNames(数学);


this works in firebug to find object methods.

Object.getOwnPropertyNames(Math);

赠我空喜 2024-09-10 12:02:45

ECMAScript 第 5 版。 定义了 Object.getOwnPropertyNames,它返回所有属性的数组传入对象的属性,包括不可枚举的属性。到目前为止,只有 Chrome 实现了这一点。

Object.getOwnPropertyNames({a: 10, b: 2});

给出 ["b", "a"] (无特定顺序)

Object.getOwnPropertyNames(Math);

给出 ["LN10", "PI", "E", "LOG10E", "SQRT2", "LOG2E ”、“SQRT1_2”、“LN2”、“cos”、“pow”、“log”、“tan”、“sqrt”、“ceil”、“asin”、“abs”、“max”、“exp”、 “atan2”,“随机”,“圆形”,“地板”,“acos”,“atan”,“分钟”,“sin”]

ECMAScript 5th ed. defines Object.getOwnPropertyNames that returns an array of all properties of the passed in object, including the ones that are non-enumerable. Only Chrome has implemented this so far.

Object.getOwnPropertyNames({a: 10, b: 2});

gives ["b", "a"] (in no particular order)

Object.getOwnPropertyNames(Math);

gives ["LN10", "PI", "E", "LOG10E", "SQRT2", "LOG2E", "SQRT1_2", "LN2", "cos", "pow", "log", "tan", "sqrt", "ceil", "asin", "abs", "max", "exp", "atan2", "random", "round", "floor", "acos", "atan", "min", "sin"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文