First of all Hello to everyone, and sorry for my English.
I would still take advantage of the expertise and availability of this community.
My question is realted to try to add properties to an object after preventExtensions method.
I know how this method works, this method makes an object not extensible, i can not add any property to this object
My question is: why if i try to add a property in this way (obj.prop), i'll find no errors in console and this property will be undefinded,
but if i try to add a new property with Object.defineproperty method i'll find an error in console?
<script>
var obj={}
Object.preventExtensions(obj)
// i can not add other properies to this object
console.log(Object.isExtensible(obj))//false
// why if i try to add a property in this way i'll find undefined
obj.b='bbb'
console.log(obj.b)
// and in this way i'll find an error
Object.defineProperty(obj, 'w', { enumerable: true,configurable:true,writable:true,value:'www' });
</script>
Basically my question is about the different behavior between obj.prop and Object.defineProperty
Any suggestions will be read with pleasure, thank you all.
I imagine this behavior is browser specific. Even the documentation seems to be vague on the topic.
Attempting to add new properties to a non-extensible object will fail, either silently or by throwing a TypeError (most commonly, but not exclusively, when in strict mode).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.