Skip to content

Instantly share code, notes, and snippets.

@aleksandar-b
Last active April 28, 2022 09:45
Show Gist options
  • Save aleksandar-b/d6e70bf292ca34bc05fedb0fae0a4e18 to your computer and use it in GitHub Desktop.
Save aleksandar-b/d6e70bf292ca34bc05fedb0fae0a4e18 to your computer and use it in GitHub Desktop.
//Value objects
//Motivation
//Use cases
//Proposed syntax
value Money {
constructor(amount, currency) {
this.amount = amount;
this.currency = currency;
}
}
const twoDollars = Money(2, '$');
const oneDollar = Money(1, '$');
//Equality is defined by their properties
twoDollars.equals(oneDollar) // false
twoDollars.equals(twoDollars) // true
//Changeless
twoDollars.amount = 4; // throws exception
//Lack causal powers.
//Values can only be measured, compared and related. Methods can only return new value or boolean.
twoDollars.updateEntity(account) // throws exception
//Concept of instance does not exist for values
new Money(1, '$') // throws exception
Money(1, '$') // all good
//FAQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment