Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Created December 17, 2015 16:47
Show Gist options
  • Save telekosmos/1dea4a23dfe4ccda8bd4 to your computer and use it in GitHub Desktop.
Save telekosmos/1dea4a23dfe4ccda8bd4 to your computer and use it in GitHub Desktop.
ES6 Dynamic accessors
describe('dynamic accessors', () => {
it('a dynamic getter name is enclosed in [ and ]', function() {
const balance = 'yourMoney';
class YourAccount {
get [balance]() { return -Infinity; }
}
assert.equal(new YourAccount().yourMoney, -Infinity);
});
it('a dynamic setter name as well', function() {
const propertyName = 'balance';
class MyAccount {
get [propertyName]() { return this.amount; }
set [propertyName](amount) { this.amount = 23; }
}
const account = new MyAccount();
account.balance = 23; // 42;
assert.equal(account.balance, 23);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment