enumerability

If we look at the way class defines prototypes, we find that the methods defined are not enumerable by default. This works around a common error where programmers iterate over the keys of an instance and fail to test for .hasOwnProperty.

我们定义一个带方法的类:

class Person { test() { } }

测试test方法的Descriptor:

    let desc = Object.getOwnPropertyDescriptor(Person.prototype, 'test')
    expect(desc.enumerable).toEqual(false)
    expect(desc.configurable).toEqual(true)
    expect(desc.writable).toEqual(true)

enumerable为假,意味着不可以用for in循环枚举:

    let obj = new Person()
    let result =[]
    for (let property in obj) result.push(property);
    expect(result).toEqual([])

原文地址:https://www.cnblogs.com/cuishengli/p/15396731.html