DEV Community

Discussion on: Conditional Function Invocation without Conditional Statements in JS

Collapse
 
webketje profile image
webketje • Edited

Object.keys makes the LogGenerator completely obsolete:

const msg = { type: 'notify', text: 'Hello World' };
Object.keys(Log)[msg.type](msg.text);
Enter fullscreen mode Exit fullscreen mode

as

class Log {
  static notify() {
    ...
  }
}
Enter fullscreen mode Exit fullscreen mode

is really just syntactic sugar for

function Log() {}
Log.notify = function() { ... }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dsasse07 profile image
Daniel Sasse

Thank you for the insight! I haven't had any experience with Object.keys() before, and my use of Class syntax in JS so far is still limited.

Thank you for the suggestion.