Introducing a fantastic higher order function called logFunctionName
that elevates the original say
function by logging its name. This incredible feat is accomplished by the enhanced enhancedSay
function, which mirrors the arguments of the initial say
function.
function say(msg: string | number) {
console.log(msg)
}
function logFunctionName<T extends (...args: any[]) => any>(func: T): (...funcArgs: Parameters<T>) => ReturnType<T> {
return (...args: Parameters<T>): ReturnType<T> => {
console.log(func.name)
return func(...args);
};
}
const enhancedSay = logFunctionName(say)("hello");
// const enhancedSay2 = messageLength(say)("hello");
// => 5
I am currently facing a challenge.
Innovation strikes again with the idea of developing a high-level messageLength
function to advance the capabilities of the say
function. This enhancement will involve converting the first argument from a string to a numerical value representing its length. Enter the enhanced enhancedSay2
function, designed for strings while the original say
function receives numbers.
UPDATE: While this example may seem straightforward, my ultimate goal is to craft a higher order function that takes a string, inserts it into the DOM, and then hands over an element to the upgraded function.