The logger
function logs a message to the console and returns a closure that can be used to print additional messages, but with a prefix of "wtf? ". The test
function demonstrates the usage of logger
by calling it and logging "wtf? undefined" to the console.
var logger = (message) => {
console.log(message)
return (m) => 'wtf? ' + m
}
function test() {
return logger `test` `another`;
}
test()
/**
* A utility function that logs a message to the console and returns a callback.
* The callback function returns a prefixed message.
* @param {string} message - The message to be logged and returned.
* @returns {function} A callback function that returns a prefixed message.
*/
function logger(message) {
// Log the message to the console
console.log(message);
// Return a callback function that prefixes the input message
return (m) => `wtf? ${m}`;
}
/**
* A function that tests the logger utility.
* @returns {void}
*/
function test() {
// Call the logger function with multiple messages
const loggerInstance = logger('test')('another');
// Log the result of the logger instance
console.log(loggerInstance);
}
// Call the test function
test();
Code Breakdown
logger
is defined that takes a message
parameter.message
to the console using console.log
.m
.m
parameter and returns the result.test
is defined that returns the result of the expression logger 'test' 'another'
.logger
function with the arguments 'test'
and 'another'
.logger
(a closure) is then called with no arguments, which results in the string "wtf? " being concatenated with undefined (since no argument is passed to the closure), and the result "wtf? undefined" being logged to the console.test
function is called, which logs "wtf? undefined" to the console.