Reputation: 88197
I am looking at some common ways of defining classes (Constructor Pattern in Book by Addy Osmani).
2 main ways I see:
Simple Prototypes:
function Person(name) { this.name = name; }
Person.prototype.getName = function() { return this.name; }
ES5 Object.create
, Object.defineProperties
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
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