Considering the basic scenario of usage, do
foo.bar = 'baz';
and
Object.defineProperty(foo, 'bar', {
value: 'baz',
configurable: true,
enumerable: true,
writable: true
});
behave exactly the same in supported browsers?
Can we fall back to vanilla in pre-ES6 applications just because of favourable syntax or mix both of them without any side effects?
Yes, they behave the same when
bar
property in foo
(not even an inherited one), so a new one is created, orbar
property that has the writable
and configurable
attributes set to true
However, if neither of those is given, the two indeed produce slightly different results.
defineProperty
does not consider inherited properties and their descriptorsdefinePropery
will overwrite the property with the data descriptor (or fail if it is an own, non-configurable one)writable
is false, or create a new own property if true, like the defineProperty
always doeswritable
is false, or set the new value if true, while defineOwnProperty
will fail iff configurable
is false and overwrite the attributes otherwise.Considering the basic scenario of usage
If by "basic usage" you mean no usage of fancy property attributes, then yes they are equivalent. Yet you should just use the simple assignments, for they are easier to read and faster to execute.
Can we fall back to vanilla in pre-ES6 applications
Notice that full support of defineProperty
comes with ES5, so unless you need to consider pre-ES5 (old IE) browsers you wouldn't care at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With