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([])