Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. There are situations in OOP where we may wish to protect access to instance properties.
  2.  
  3. In the following example we will make a bank account constructor in the hopes that by including a pin, will be protected.
  4.  
  5. ```javascript
  6. const BankAccount = function(initialBalance, pin) {
  7. this.balance = initialBalance;
  8. this.pin = pin;
  9. }
  10.  
  11. BankAccount.prototype.withdrawl = function(pin, amount) {
  12. if (pin === this.pin) {
  13. this.balance -= amount;
  14. }
  15. }
  16.  
  17. BankAccount.prototype.deposit = function(pin, amount) {
  18. if (pin === this.pin) {
  19. this.balance += amount;
  20. }
  21. }
  22.  
  23. let account = new BankAccount(100.00, 1234);
  24. ```
  25.  
  26. Unfortunately, anyone can access both the pin and the balance directly, which is a big security problem.
  27.  
  28. ```javascript
  29. let stolenPin = account.pin;
  30. let stolenMoney = account.balance
  31. account.balance = 0;
  32.  
  33. console.log(`Here is the stolen PIN: ${stolenPin}`);
  34. console.log(`Here is the stolen money: ${stolenMoney}`);
  35. ```
  36.  
  37.  
  38. Rather than definine properties that we want to be private on `this` where they can be accessed by anyone, we declare them as varables.
  39.  
  40. In conjunction with defining the properties as variables, we must now define our functions within the constructor, instead of on the constructor's prototype, so that they are closures, with continued acces to the private variables.
  41.  
  42. ```javascript
  43. const SecureBankAccount = function(initialBalance, pin) {
  44. // balance is NOT defined as a property on `this`
  45. let balance = initialBalance;
  46. // pin is also defined because it is passed in as an argument
  47.  
  48. // These functions can access `balance` and `pin` because they are closures.
  49. // In order to create these closures, we cannot define them on the
  50. // constructor's `prototype`
  51. this.withdrawl = function(pinAttempt, amount) {
  52. if (pinAttempt === pin) {
  53. balance -= amount;
  54. }
  55. }
  56.  
  57. this.deposit = function(pinAttempt, amount) {
  58. if (pinAttempt === pin) {
  59. balance += amount;
  60. }
  61. }
  62.  
  63. this.getBalance = function(pinAttempt) {
  64. if (pinAttempt === pin) {
  65. return balance;
  66. }
  67. }
  68. }
  69.  
  70. let secureAccount = new SecureBankAccount(100, 1234)
  71.  
  72. let foiledAttemptAtStolenPin = secureAccount.pin; // foiledAttemptAtStolenPin is undefined
  73. let foiledAttemptAtStolenMoney = secureAccount.balance; // foiledAttemptAtStolenMoney is undefined
  74.  
  75. console.log(`Here is the foiled attempt at stealing the pin: ${foiledAttemptAtStolenPin}`);
  76. console.log(`Here is the foiled attempt at stealing the money: ${foiledAttemptAtStolenMoney}`);
  77. ```
  78.  
  79.  
  80. Using the instance methods, which are closures, we can interact with the private variables as intended.
  81.  
  82. ```javascript
  83. let initialBalance = secureAccount.getBalance(1234);
  84. console.log(`Here is the initial balance after passing in the correct pin: ${initialBalance}`);
  85.  
  86. secureAccount.deposit(1234, 100);
  87. let balanceAfterDeposit = secureAccount.getBalance(1234);
  88. console.log(`Here is the balance after deposit after passing in the correct pin: ${balanceAfterDeposit}`);
  89. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement