Hiding the calling function in JavaScript

I need to write an application that runs some mixed JavaScript code. What I mean by โ€œmixedโ€ is that some of the code is mine and some are external. My code will call some external code, but I would like to hide the call stack. In other words, in such a scenario:

// my code
function myFunc()
{
   extFunc();
}

// external code
function extFunc()
{
   if (arguments.callee.caller == null)
   {
        console.log("okay");
   }
}

I would like the last "if" to evaluate to true. Can this be done in plain JavaScript?

+4
source share
2 answers

Functions defined in strict mode do not have a property caller.

See the following code in the console:

function a() {
    return arguments.callee.caller;
}
(function b(){
    return a()
}()) // this expression returns b function
var c = (function strict(){
  'use strict';
  return function cInner() {
     return a();
  }
})();

c(); // Throw TypeError: access to strict mode caller function is censored

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

+2

async:

function myFunc()
{
   setTimeout(function(){ extFunc(); }, 0);
}
+1

Source: https://habr.com/ru/post/1609488/


All Articles