Created
December 17, 2015 16:47
-
-
Save telekosmos/1dea4a23dfe4ccda8bd4 to your computer and use it in GitHub Desktop.
ES6 Dynamic accessors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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