Hard
This question is a daily question and will count towards your daily streak.
How can you dynamically add methods to a JavaScript class at runtime?
1class Calculator {2 constructor() {3 this.value = 0;4 }5}67const calculator = new Calculator();89const operations = {10 add(num) {11 this.value += num;12 return this.value;13 },14 subtract(num) {15 this.value -= num;16 return this.value;17 },18};1920Object.keys(operations).forEach((op) => {21 Calculator.prototype[op] = operations[op];22});2324console.log(calculator.add(10));25console.log(calculator.subtract(5));