Why does for(var i in Math) not iterate through Math.* in Javascript?

前端 未结 4 721
旧巷少年郎
旧巷少年郎 2021-02-14 11:19

For some reason for(var i in Math){console.log(i)} doesn\'t show the expected tan, cos, atan2, E, PI in Javascript.

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 11:46

    Those properties are non-enumerable.

    From the MDC documentation on for..in:

    A for...in loop does not iterate over built-in properties.

    In newer JavaScript implementations you can make your own non-enumerable properties. Check out propertyIsEnumerable() and Object.defineProperty().

提交回复
热议问题