SHARE
TWEET

Untitled

a guest Apr 19th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 23: class - accessors
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3. // Follow the hints of the failure messages!
  4.  
  5. describe('Class accessors (getter and setter)', () => {
  6.   it('a getter is defined like a method prefixed with `get`', () => {
  7.     class MyAccount {
  8.       get balance() { return Infinity; }
  9.     }
  10.     assert.equal(new MyAccount().balance, Infinity);
  11.   });
  12.   it('a setter has the prefix `set`', () => {
  13.     class MyAccount {
  14.       get balance() { return this.amount; }
  15.       set balance(amount) { this.amount = amount; }
  16.     }
  17.     const account = new MyAccount();
  18.     account.balance = 23;
  19.     assert.equal(account.balance, 23);
  20.   });
  21.  
  22.   describe('dynamic accessors', () => {
  23.     it('a dynamic getter name is enclosed in `[]`', function() {
  24.       const balance = 'yourMoney';
  25.       class YourAccount {
  26.         get [balance]() { return -Infinity; }
  27.       }
  28.       assert.equal(new YourAccount().yourMoney, -Infinity);
  29.     });
  30.     it('a dynamic setter name as well', function() {
  31.       const propertyName = 'balance';
  32.       class MyAccount {
  33.         get [propertyName]() { return this.amount; }
  34.         set [propertyName](amount) { this.amount = 23; }
  35.       }
  36.       const account = new MyAccount();
  37.       account.balance = 23;
  38.       assert.equal(account.balance, 23);
  39.     });
  40.   });
  41. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top