Guest User

Untitled

a guest
Jul 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. // 23: class - accessors
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3.  
  4. describe('class accessors (getter and setter)', () => {
  5.  
  6. it('only a getter is defined like a method prefixed with `get`', () => {
  7. class MyAccount {
  8. get balance() { return Infinity; }
  9. }
  10.  
  11. assert.equal(new MyAccount().balance, Infinity);
  12. });
  13.  
  14. it('a setter has the prefix `set`', () => {
  15. class MyAccount {
  16. get balance() { return this.amount; }
  17. set balance(amount) { this.amount = amount; }
  18. }
  19.  
  20. const account = new MyAccount();
  21. account.balance = 23;
  22. assert.equal(account.balance, 23);
  23. });
  24.  
  25. describe('dynamic accessors', () => {
  26.  
  27. it('a dynamic getter name is enclosed in [ and ]', function() {
  28. const balance = 'yourMoney';
  29. class YourAccount {
  30. get [balance]() { return -Infinity; }
  31. }
  32.  
  33. assert.equal(new YourAccount().yourMoney, -Infinity);
  34. });
  35.  
  36. it('a dynamic setter name as well', function() {
  37. const propertyName = 'balance';
  38. class MyAccount {
  39. get [propertyName]() { return this.amount; }
  40. set [propertyName] (amount) { this.amount = 23; }
  41. }
  42.  
  43. const account = new MyAccount();
  44. account.balance = 42;
  45. assert.equal(account.balance, 23);
  46. });
  47. });
  48.  
  49. });
Add Comment
Please, Sign In to add comment