如何列出Math对象的所有属性? [英] How can I list all the properties of Math object?

查看:73
本文介绍了如何列出Math对象的所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我

for (var i in window) console.log(window[i])

我得到一个窗口属性和方法列表

I get a list of window properties and methods

Howver当我为数学对象做同样的事情时,我什么也得不到。

Howver when I do the same for "Math" object, I get nothing.

typeof "window" == typeof "Math"

返回 TRUE ,所以我看不出我的循环没有的原因工作。

returns TRUE, so I do not see a reason why my loop is not working.

这很奇怪,好像我直接写数学['E'] 我得到常数的值 E

It's strange as if I write directly Math['E'] I get the value of constant E.

更新

谢谢。所有答案都很有帮助,并提供了我想要的。我对它们中的大多数进行了投票。

Thank you. All the answers were helpful and provided exactly what I wanted. I upvoted most of them.

推荐答案

并非所有对象属性都是可迭代的。您只能在 for..in 循环中获得可迭代属性。

Not all object properties are iterable. You'll only get iterable properties in a for..in loop.

由于<$ c的大部分属性$ c> window (恰好是全局对象)是用户定义的全局变量,它们是可枚举的。

Since most properties of window (which happens to be the global object) are user-defined global variables, they are enumerable.

在现代JavaScript引擎中您可以使用 Object.getOwnPropertyNames(obj)来获取所有可枚举和不可枚举的属性:

In modern JavaScript engines you can use Object.getOwnPropertyNames(obj) to get all properties, both enumerable and non-enumberable:

>>> Object.getOwnPropertyNames(Math)
["toSource", "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp", "floor", "log", "max", "min", "pow", "random", "round", "sin", "sqrt", "tan", "E", "LOG2E", "LOG10E", "LN2", "LN10", "PI", "SQRT2", "SQRT1_2"]

参见< a href =https://stackoverflow.com/questions/8024149/is-it-possible-to-get-the-non-enumerable-inherited-property-names-of-an-object>是否有可能得到对象的不可枚举的继承属性名称?以获取更多详细信息。

See Is it possible to get the non-enumerable inherited property names of an object? for more details.

这篇关于如何列出Math对象的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆