javascript - About defineProperty's value
给我你的怀抱
给我你的怀抱 2017-06-26 10:54:03
0
2
650
> var a = {}
> Object.defineProperty(a, "b", {
... value: 110})
{}
> a
{}
> a.b
110
> a.b = 555
555
> a.b
110

> var aa = {b: 1}
undefined
> Object.defineProperty(aa, "b", {
... value: 119})
{ b: 119 }
> aa.b
119
> aa.b = 1
1
> aa.b
1

Why can aa.b be changed but a.b cannot?

给我你的怀抱
给我你的怀抱

reply all(2)
某草草

The property descriptor has three properties: configurable enumerable writable. The default values ​​​​are all false
, while the property descriptors defined using object literals are all true, which can be passed Object.getOwnPropertyDescriptor( a, 'b') and Object.getOwnPropertyDescriptor(aa, 'b') to get the descriptors of two properties.

So a.b cannot be modified, aa.b can be modified.

迷茫

Because by default, the property values ​​of properties defined through Object.defineProperty() are all false and are not writable.

a = {}
Object.getOwnPropertyDescriptor(a, 'b')
// > undefined
Object.defineProperty(a, "b", {value: 119})
Object.getOwnPropertyDescriptor(a, 'b')
// > Object {value: 119, writable: false, enumerable: false, configurable: false}

The properties defined through object literals are writable by default, call `

aa = { b: 1 }
Object.getOwnPropertyDescriptor(aa, 'b')
// > Object {value: 1, writable: true, enumerable: true, configurable: true}

So the b property of aa is writable. So Object.defineProperty() does not change the attribute value of the property. So the value of b of aa will change.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template