Skip to content

Latest commit

 

History

History
17 lines (12 loc) · 418 Bytes

20201213.md

File metadata and controls

17 lines (12 loc) · 418 Bytes

下面代码输出是什么?为什么?

const person = { name: '66' }
Object.defineProperty(person, 'age', { value: 21 })
person.age = 18
console.log(person)
console.log(Object.keys(person))

答案

{ name: '66', age: 21 } ['name']

解析

使用Object.defineProperty方法添加的属性默认是不可枚举和不可变的。可以通过writable,configurable和enumerable来改变这一行为