Tip 40 | Simplify Interfaces with get and set |
In this tip, you’ll learn how to mask logic behind simple interfaces with get and set.
You have the basics of classes. You can create instances, call properties, call methods, and extend parent classes. But it won’t be long before someone tries to alter a property you had no intention of exposing. Or maybe someone sets the wrong data type on a property, creating bugs because the code expects an integer, not a string.
One of the major problems in JavaScript is that there are no private properties by default. Everything is exposed. You can’t control what the users of your class do with the methods or properties.
Think about your Coupon. It has a property of price, which you initially set in the constructor. ...
Get Simplifying JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.