Guest User

Untitled

a guest
Dec 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. describe('Class accessors (getter and setter)', () => {
  2. it('a getter is defined like a method prefixed with `get`', () => {
  3. class MyAccount {
  4. get money() { return Infinity; }
  5. get balance() {return this.money; }
  6. }
  7. assert.equal(new MyAccount().balance, Infinity);
  8. });
  9. it('a setter has the prefix `set`', () => {
  10. class MyAccount {
  11. get balance() { return this.amount; }
  12. set balance(amount) { this.amount = amount; }
  13. }
  14. const account = new MyAccount();
  15. account.balance = 42;
  16. assert.equal(account.balance, 42);
  17. });
  18.  
  19. describe('dynamic accessors', () => {
  20. it('a dynamic getter name is enclosed in `[]`', function() {
  21. const balance = 'yourMoney';
  22. class YourAccount {
  23. get [balance]() { return -Infinity; }
  24. }
  25. assert.equal(new YourAccount().yourMoney, -Infinity);
  26. });
  27. it('a dynamic setter name as well', function() {
  28. const propertyName = 'balance';
  29. class MyAccount {
  30. get [propertyName]() { return this.amount; }
  31. set [propertyName](amount) { this.amount = 23; }
  32. }
  33. const account = new MyAccount();
  34. account.balance = 42;
  35. assert.equal(account.balance, 23);
  36. });
  37. });
  38. });
Add Comment
Please, Sign In to add comment