Jiew Meng
Jiew Meng

Reputation: 88197

Defining Classes in JS (ES5 vs Prototype)

I am looking at some common ways of defining classes (Constructor Pattern in Book by Addy Osmani).

2 main ways I see:

I am wondering, why might I even consider the ES5 way which appears to be alot more complicated? Is there any advantages? Maybe the only main advantage is having read-only properties like some strongly typed language?

Upvotes: 4

Views: 3723

Answers (1)

Alnitak
Alnitak

Reputation: 339816

Object.defineProperties is only needed if you want those properties to have particular attributes, e.g. non-enumerable, read-only, or with getters or setter functions.

There's no need to use it for normal classes and their methods, where the "simple prototype" method you describe is perfectly adequate.

Upvotes: 4

Related Questions