wildernesscat
wildernesscat

Reputation: 41

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 is external. My code will be calling some external code, but I would like to conceal the call stack. In other words, in a scenario like this:

// 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 true. Can it be done in plain JavaScript?

Upvotes: 4

Views: 78

Answers (2)

xFight
xFight

Reputation: 68

You could try to call the method async:

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

Upvotes: 1

Ginden
Ginden

Reputation: 5316

Functions defined in strict mode does not have caller property.

See following code in 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

More about strict mode - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

Upvotes: 2

Related Questions