9.3. Methods only real instances can call
Topics
This question is in
9. Prototypes and classesA payment form adds prices together, and a bug elsewhere passes in a plain object { amount: 1050, currency: "USD" }, or an object made with Object.create(Money.prototype). Both pass an instanceof Money check, and both would be added as if they were real. A brand check makes a class's methods refuse any object that its constructor did not build: they throw a TypeError instead of computing with data nobody validated.
Write the class Money in money.js. It holds a whole number of cents and a currency code, and every method, getter and the static Money.isMoney accept only objects that new Money (or a subclass's super) built.
Signature
class Money {
constructor(amount, currency)
get amount() // the cents
get currency() // the code, like "USD"
format() // "10.50 USD"
add(other) // a new Money
static isMoney(value) // true or false
}amount: a whole number of cents; negative amounts are allowed.currency: exactly three capital letters A to Z.
Requirements
amountandcurrencyreturn what the constructor received.format()returns the amount in whole units with two decimals, a space and the currency:1050gives"10.50 USD",-5gives"-0.05 USD".- A
Moneyobject has no own properties, and assigningamountorcurrencyfrom outside does not change them. format,addand the getters throw aTypeErrorwhenthisis not an object thatnew Moneybuilt: a plain look-alike object, an object made withObject.create(Money.prototype), aProxyaround a realMoney, orundefined(a method taken off its object).- A real
Moneystays real when its prototype is changed:Money.prototype.format.call(money)still works afterObject.setPrototypeOf(money, null). - Objects built by a subclass (
class Price extends Money) are realMoneyobjects. Money.isMoney(value)returnstrueonly for realMoneyobjects andfalsefor everything else, including primitives,null,undefined,Moneyitself andMoney.prototype. It never throws.add(other)returns a new realMoneywith the sum and the same currency, and changes neither input.addthrows aTypeErrorwhenotheris not a realMoney, and aRangeErrorwhen the currencies differ.- The constructor throws a
TypeErrorwhenamountis not an integer (1.5,"100",NaN,Infinity) orcurrencyis not three capital letters ("usd","US",840, missing). CallingMoneywithoutnewthrows aTypeError.
Examples
const price = new Money(1050, "USD");
price.format(); // "10.50 USD"
price.add(new Money(-50, "USD")).amount; // 1000
Money.isMoney(price); // trueconst impostor = Object.create(Money.prototype);
impostor instanceof Money; // true, but:
impostor.format(); // TypeError
Money.isMoney(impostor); // false
Money.isMoney({ amount: 1, currency: "USD" }); // falseconst price = new Money(250, "USD");
Object.setPrototypeOf(price, null);
Money.prototype.format.call(price); // "2.50 USD"
price.add(new Money(1, "EUR")); // TypeError: price has no add now
new Money(250, "USD").add(new Money(1, "EUR")); // RangeErrorEnvironment
- Plain JavaScript in a browser page; the tests use the global class
Moneyfrommoney.js. instanceofand prototype checks are not enough: requirement 4 includes an object whose prototype isMoney.prototype.
Hints
- What does reading a private field do when the object does not have that field?
- Which operator asks "does this object have this private field?" without throwing, and what does it need its right side to be?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it