June 2018
Beginner
288 pages
6h 31m
English
Here are the solutions to the Exercises, for the Chapter 12, Deep Dive into Metaprogramming chapter.
| | 'use strict'; |
| | |
| | class Counter { |
| | constructor() { |
| | this.count = 0; |
| | } |
| | |
| | incrementBy(value) { this.count += value; return this.count; } |
| | decrementBy(value) { this.count -= value; return this.count; } |
| | } |
| | |
| | const call = function(counter, method, ...data) { |
| | const methodToCall = Reflect.get(counter, method); |
| | return Reflect.apply(methodToCall, counter, data); |
| | }; |
| | |
| | const counter = new Counter(); |
| | |
| | console.log(call(counter, 'incrementBy', 10)); //10 |
| | console.log(call(counter, 'decrementBy', 7)); //3 |
| | console.log(counter.count); //3 |
Read now
Unlock full access