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 bar property in foo (not inherited one), new 1 created, or
  • there bar property has writable , configurable attributes set true

however, if neither of given, 2 indeed produce different results.

  • defineproperty not consider inherited properties , descriptors
  • if existing (possibly inherited) property accessor, assignment try call setter (and fail if none exists), while definepropery overwrite property data descriptor (or fail if own, non-configurable one)
  • if existing inherited property data property, assignment fail if writable false, or create new own property if true, defineproperty does
  • if existing own property data property, assignment fail if writable false, or set new value if true, while defineownproperty fail iff configurable false , 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

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -