使用下划线检查自己的属性

Using Underscore to check own property

本文关键字:自己的 属性 检查 下划线      更新时间:2023-09-26

用于Underscore检查global对象是否拥有Nodejs控制台上的parseInt函数,

U = require('underscore')
U.contains(U.keys(global), 'parseInt') // false
U.has(global, 'parseInt') // true

为什么上面给出了相反的结果?

Object.keys 返回其描述符标记为 enumerable 的对象属性。在这种情况下,parseInt不可枚举:

例如

Object.getOwnPropertyDescriptor(global, 'parseInt')

{
    "writable":true,
    "enumerable":false,
    "configurable":true,
    "value": function parseInt(){ ...}
}