January 2020
Intermediate to advanced
470 pages
11h 13m
English
How would we write this binaryOp() function? There are at least two ways of doing so: a safe but long one and a riskier and shorter alternative. The first would require listing each possible operator. The following code does this by using a longish switch:
const binaryOp1 = op => { switch (op) { case "+": return (x, y) => x + y; case "-": return (x, y) => x - y; case "*": return (x, y) => x * y; // // etc. // }};
This solution is perfectly fine but requires too much work. The second is more dangerous, but shorter. Please consider this just as an example, for learning purposes; using eval() isn't recommended, for security reasons! Our second version would simply use Function() to create a new function that uses the ...
Read now
Unlock full access