DEV Community

Discussion on: JavaScript Bites: Closure

Collapse
 
peerreynders profile image
peerreynders • Edited

I think I need to come up with a better example in this case to show the partial application, as the underlying console.log only takes the one argument.

const docLogger = mkLogger('DOCS', true);

logger('LIBS', 'Direct log message', false);
docLogger('This is my log message');
docLogger('Another log message');

function logger(route, message, showDate) {
  const header = showDate ? `${new Date().toISOString()} | ${route}` : route;
  console.log(`${header} | ${message}`);
}

function mkLogger(route, showDate = false) {
  // Implement "partial application" with the values
  // in the closure
  return (message) => logger(route, message, showDate);
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
bamartindev profile image
Brett Martin

Thanks for writing this up - ill add it to the main article and shout you out!