Reputation: 12690
Other than coding style is there any advantages/disadvantages to either:
Circle.prototype = { radius : 10};
Object.defineProperty(Circle.prototype, 'circumference', {
get: function() { return 2*Math.PI*this.radius; }
});
vs
Circle.prototype = {
radius : 10,
get circumference() { return 2*Math.PI*this.radius; }
}
Upvotes: 3
Views: 97
Reputation: 6005
In property definition, JavaScript handles it via the internal method DefineOwnProperty
, where assignment is handled by the internal method Put
. In short, the second one checks whether or not a property is read-only and if yes, leads to rejection.
This can have consequences when using read-only properties, which prevent assignment, but not definition.
If you want to create a new property it is better use definition. If you want to change the value of a property, you can use assignment.
Take a look here for more, very interesting article.
EDIT: In fact, defineProperty
is used for reasons such as defining read only properties and other reasons about the behavior of the property defined.
Take a look here for more too.
Upvotes: 2