0

I have some old code and I need to refactor/rewrite arguments.callee.caller.caller and arguments.callee.caller in two funcs to be able to 'use strict'. Can be max up to ECMAScript 2019, thanks!

const utils = {
/**
 * @return {string}
 */
caller: function (string, callerName) {
    const caller = /^function\s+([^(]+)/.exec(/** @type{string} */ arguments.callee.caller.caller.toString());
    return caller ? caller[1] : '';
},
/**
 * @return {string}
 */
function_name: function () {
    const caller = /^function\s+([^(]+)/.exec(/** @type{string} */ arguments.callee.caller.toString());
    return caller ? caller[1] : '';
}

};

  • Warning: The 5th edition of ECMAScript (ES5) forbids use of arguments.callee() in strict mode. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee – IT goldman Jun 22 '22 at 19:16
  • AFAIK there is no strict mode safe way to get information about the caller. – Felix Kling Jun 22 '22 at 19:41
  • Simply pass in the function (name) explicitly where it is really needed. Get rid of this `utils` object. – Bergi Jun 23 '22 at 01:50
  • @Bergi, I can't do that because utils is linked with 100 other funcs, example: ``` function on_char(code) { console.log(utils.function_name()); } function buttonEventHandler(x, y, m) { const c = utils.caller(); } ``` – German_Detlev Jun 23 '22 at 10:12
  • @German_Detlev yes you can, or rather, yes you will need to. Write `console.log("on_char")` and `function buttonEventHandler(x, y, m, c = "") {…}` instead. If it's only a hundred occurrences, you can quickly do this by hand, if it's more, an automated codemod might help you. There's no way around modernising your code. – Bergi Jun 23 '22 at 18:53

0 Answers0