0

I'm trying to assign function to multiple variables, the function calls another function getFuncName(), I want getFuncName to return the name of the function that got invoked.

function getFuncName() {
    return getFuncName.caller.name;
}

const error = warning = info = verbose = debug = silly = message => {
        console.log(getFuncName())
}

When error gets invoked it prints silly, same for warning, info, verbose and debug. I want that if error gets invoked, it will print error, same for every other variable, Thanks.

1
  • 1
    Be aware that the const declaration in your sample code declares one constant, error. The other names ("warning", "info", "verbose", etc) are not declared as constants. Instead they are interpreted as simple references to variables declared elsewhere., Commented Aug 26, 2019 at 13:17

4 Answers 4

1

The only way you do it is to:

const error = message => {
  console.log(getFuncName())
}

const warning = error
const info = error
// etc

On the other hand why would you want to do this? Maybe you wanted to push a log level or something? In this case you could do like this (higher-order functions):

const logger = level => message => {
  console.log(level, ': ', getFuncName())
}

const error = logger('error')
// etc
Sign up to request clarification or add additional context in comments.

Comments

1

Don't use Function.caller, it's non-standard and does not behave consistently across browsers. It also does not work in strict mode:

'use strict';

function foo () {
  function bar () {
    console.log(bar.caller.name);
  }
  
  return bar();
}

foo();

Just use partial application to create a closure for message => { ... }:

'use strict';

const logger = type => message => {
  console.log(type, message);
};

const error = logger('error');
const warning = logger('warning');
const info = logger('info');
const verbose = logger('verbose');
const debug = logger('debug');
const silly = logger('silly');

error('foo');
info('bar');

Comments

0

In your code, besides getFuncName, there is only a single function in existence. It's referred to via 6 different variables, but there is only one function, with one name.

You can't expect the same function to have different names at different times. Anonymous functions can be given a default name by the interpreter when they appear in the code assigned to a variable - in the case that the anonymous function is assigned to multiple variables at once, the rightmost variable name ("silly", in your case) is considered the name of the function.

Comments

-1

A one-liner solution from @Patrick Roberts's answer:

const [error, warning, info, verbose, debug, silly] = ['error', 'warning', 'info', 'verbose', 'debug', 'silly'].map(type => msg => {console.log(type, msg);});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.