During the "bootcamp" phase of the web-development program in which I am currently a student, we learned about closure. Coming from an object-oriented programming language, the concept was not new. Many of my fellow students, however, struggled not with how the feature works but why it is important. In this article I will use a java class to display two primary use cases.
The following class describes a bank account. There are four properties: username, password, balance, and isLoggedIn. IsLoggedIn is set to true when a user logs into their account and false after the user logs out. There are four methods: login, logout, getBalance, and withdraw. Login and logout set the isLoggedIn property. GetBalance returns the balance property and withdraw sets the balance property to the current value minus the number passed into the method.
The same code can be written in javascript:
An instantiation of a java class is similar to a javascript object. It contains properties (called instance variables) and methods that act upon those properties. Properties preceded by the reserved keyword private can only be directly accessed within the class. Client code has access to private properties via public methods.
In the example class, the only way to access the balance is to first pass the proper credentials into the login method. After logging in, a call to getBalance will return the balance of the account.
The following code throws a compile-time error because the client code is attempting to directly access the private instance variable balance.
The same thing happens when you attempt to directly access balance in the javascript object. An error is not thrown. However, const balance is undefined.
The following client code does not throw an error because balance is being accessed via the public method getBalance:
Here is the javascript version. As you can see, const balance is to 500 dollars.
Why is closure so important?
Closure protects sensitive data. Without it, client code has direct access to every property and method on the object. In this example, in order to access the balance of the account or make a withdraw, a correct username and password must be passed into the login method. In the real world, the user would be authenticated prior to logging in. Prior to getting or setting the balance, authorization code would ensure they have permission to do so.
Closure also ensures that client code follows the rules set forth in the class. In the example class, after successfully logging into the account, if the user tries to withdraw an amount greater than the balance of the account, an error is thrown. If client code had direct access to balance, it could change the value of balance to a negative number. In the constructor of the example class, instead of simply setting the password equal to the password provided, the constructor could make sure it met complexity requirements (capital letters, numbers, symbols, length, etc.) All clients would be forced to comply with these rules.
Top comments (0)