javascript - Object.defineProperty vs vanilla property -
considering basic scenario of usage, do
foo.bar = 'baz'; and
object.defineproperty(foo, 'bar', { value: 'baz', configurable: true, enumerable: true, writable: true }); behave same in supported browsers?
can fall vanilla in pre-es6 applications because of favourable syntax or mix both of them without side effects?
yes, behave same when
- there no
barproperty infoo(not inherited one), new 1 created, or - there
barproperty haswritable,configurableattributes settrue
however, if neither of given, 2 indeed produce different results.
definepropertynot consider inherited properties , descriptors- if existing (possibly inherited) property accessor, assignment try call setter (and fail if none exists), while
defineproperyoverwrite property data descriptor (or fail if own, non-configurable one) - if existing inherited property data property, assignment fail if
writablefalse, or create new own property if true,definepropertydoes - if existing own property data property, assignment fail if
writablefalse, or set new value if true, whiledefineownpropertyfail iffconfigurablefalse , overwrite attributes otherwise.
considering basic scenario of usage
if "basic usage" mean no usage of fancy property attributes, yes equivalent. yet should use simple assignments, easier read , faster execute.
can fall vanilla in pre-es6 applications
notice full support of defineproperty comes es5, unless need consider pre-es5 (old ie) browsers wouldn't care @ all.
Comments
Post a Comment