2

I have a general question on the practical usage of javascript's ecmascript 5 methods.

e.g.

Object.defineProperties(obj, value, config)

to my knowledge javascript is the driver on the front-end web application. There isn't really a lot of usage in defining variable constant? I can see how enumerable might come in handy if you were to classify attributes. But why is defineProperty so verbose?

and for getters and setters... who would want to hide this functionality inside ecmascript 5, if some old browser runs ecma3, the getter function might not even occur. Why not just use a plain getter function to achieve the same? rather than risking behavior?

user2167582
  • 5,986
  • 13
  • 64
  • 121

1 Answers1

2

First, your syntax is wrong, you are mixing defineProperty and defineProperties. The proper ways are:

Object.defineProperties(obj, props)
Object.defineProperty(obj, prop, descriptor)

There isn't really a lot of usage in defining variable constant?

Well, @danronmoon disagrees.

I can see how enumerable might come in handy

Yes, I think non enumerable properties are perfect if you want to add methods to constructors like Object, but don't want for...in lops to iterate them.

But why is defineProperty so verbose

I don't find it too verbose. If you mean that the generally preferred way is writable: true, configurable:true, enumerable:true but they default to false, I guess that's because then you can just create the property using dot or bracket notation

if some old browser runs ecma3, the getter function might not even occur

Of course, but following this logic web technologies wouldn't evolve.

Why not just use a plain getter function to achieve the same

Because it's more cool, flexible, powerful, ...

For example, they make it easier to make shims for missing native properties. How would you polyfill classList property without getters?

Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • well, I completely agree with enhancing features, just that browser compatibility at my work place has always been put higher emphasis than using ecma5 for best practices. and since polyfill is propbably not possible, they wont like what I am learning... – user2167582 Jun 21 '14 at 04:50
  • 1
    @user2167582 If browser compatibility is so important at your work place, then it may be better not using `defineProperty`. But for other people it can be very useful. – Oriol Jun 21 '14 at 13:55
  • So, to sum it up, if you don't want **anything** to ever happen to your precious value, use `defineProperties`, else, use old-school properties. right? – vsync Aug 07 '15 at 13:12
  • @vsync Yes, if you want a configurable, writable and enumerable property, the old way is simpler. Otherwise, the new way is more flexible. – Oriol Aug 07 '15 at 15:09