Chapter 8
Here are the solutions to the Exercises , for the Chapter 8, Using Inheritance chapter.
Exercise 1 'use strict' ; class FunctionalSet extends Set { filter(predicate) { return new FunctionalSet([...this ].filter(predicate)); } map(mapper) { return new FunctionalSet([...this ].map(mapper)); }
reduce(accumulator, identity) { return [...this ].reduce(accumulator, identity); } } const set = new FunctionalSet(['Jack' , 'Jill' , 'Tom' , 'Jerry' ]); const jSet = set .filter(name => name.startsWith('J' )); const allCaps = set .map(name => name.toUpperCase()); const totalLengthOfJWords = set .filter(name => name.startsWith('J' )) .reduce((total, word) => total + word.length, 0); console.log(jSet); console.log(allCaps); console.log(totalLengthOfJWords);
Exercise 2
No code change is necessary. The add() method of Set returns an instance based on the runtime type. When add() is called on an instance of FunctionalSet , the returned instance is of the same derived type.
Exercise 3 'use strict' ; class BoundedSet extends Set { constructor (capacity, initialValues) { super (); this .capacity = capacity; if (initialValues.length <= capacity) { initialValues.forEach(value => this .add(value)); } } add(value) { if (this .has(value)) return ; if (this .size < this .capacity) { super .add(value); } else { throw new Error(`exceeded capacity of ${this .capacity} elements` ); } } } const set = new BoundedSet(5, ['Apple' , 'Banana' , 'Grape' , 'Mangoe' ]); set .add('Orange' ); set .add('Apple' );
try { set .add('Tangerine' ); } catch (ex) { console.log(ex.message); } set .delete ('Grape' ); set .add('Peach' ); console.log(set .size); const set2 = new BoundedSet(2, ['Apple' , 'Banana' , 'Grape' ]); console.log(set2.size); console.log(set2);
Exercise 4 'use strict' ; class Base { copy() { const constructor = Reflect.getPrototypeOf(this ).constructor [Symbol.species] || Reflect.getPrototypeOf(this ).constructor ; return new constructor (); } } class Derived1 extends Base { static get [Symbol.species]() { return Base; } } class Derived2 extends Base { static get [Symbol.species]() { return Derived2; } } const derived1 = new Derived1(); const derived2 = new Derived2(); console.log(derived1.copy()); console.log(derived2.copy());
Exercise 5 'use strict' ; class SpecialWordChecker { isSpecial(word) { return word !== word; } }
class PalindromeChecker extends SpecialWordChecker { isSpecial(word) { return [...word].reverse().join('' ) === word || super .isSpecial(word); } } class AlphabeticalChecker extends SpecialWordChecker { isSpecial(word) { return [...word].sort().join('' ) === word || super .isSpecial(word); } } const checkIfSpecial = function (specialWordChecker, word) { const result = specialWordChecker.isSpecial(word) ? 'is' : 'is not' ; console.log(` ${word} ${result} special` ); }; const palindromeChecker = new PalindromeChecker(); checkIfSpecial(palindromeChecker, 'mom' ); checkIfSpecial(palindromeChecker, 'abe' ); const alphabeticalChecker = new AlphabeticalChecker(); checkIfSpecial(alphabeticalChecker, 'mom' ); checkIfSpecial(alphabeticalChecker, 'abe' ); const alphabeticalAndPalindromeChecker = Object.setPrototypeOf( Object.getPrototypeOf(new AlphabeticalChecker()), new PalindromeChecker()); checkIfSpecial(alphabeticalAndPalindromeChecker, 'mom' ); checkIfSpecial(alphabeticalAndPalindromeChecker, 'abe' );
..................Content has been hidden....................
You can't read the all page of ebook, please click
here login for view all page.