可枚举属性是对象中可被 for...in 循环遍历到的属性。
let obj = {name: "Andy", age: 18}; for(let key in obj) { console.log(key); } //name //age
很显然,obj这个对象中,name和age都是可枚举属性。
我们说,可枚举属性是可以被 for...in 循环遍历出来的属性,那么“不可枚举属性”就是指不能被 for...in 循环遍历出来的属性。
let obj = {name: "Andy", age: 18}; Object.defineProperty(obj, 'gender', { value: 'male', enumerable: false }); for(let key in obj) { console.log(key); } //name //age
我们可以发现,gender这个“不可枚举属性”就不能被 for...in 循环遍历出来。