javascript – 如何列出Math对象的所有属性?

前端之家收集整理的这篇文章主要介绍了javascript – 如何列出Math对象的所有属性?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我做
for (var i in window) console.log(window[i])

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

当我对“数学”对象做同样的事情时,我什么也没有.

typeof "window" == typeof "Math"

返回TRUE,所以我没有看到为什么我的循环不起作用的原因.

很奇怪,就像我直接写Math [E]我得到常数E的值.

UPDATE

谢谢.所有的答案都是有帮助的,并提供了我想要的.我高涨了大部分.

解决方法

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

由于窗口的大部分属性(恰好是全局对象)都是用户定义的全局变量,因此它们是可列举的.

在现代JavaScript引擎中,您可以使用Object.getOwnPropertyNames(obj)来获取所有属性,包括枚举和非可序列化:

>>> 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"]

详见Is it possible to get the non-enumerable inherited property names of an object?.

原文链接:https://www.f2er.com/js/152543.html

猜你在找的JavaScript相关文章