4

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?

3
  • 4
    Out of curiousity, why do you want to do this? Is it some "security" concern? Commented Sep 29, 2015 at 13:58
  • You could it call it in a setTimeout like setTimeout(extFunc, 0);. Commented Sep 29, 2015 at 14:02
  • @RocketHazmat Yes, it is for reasons of security. I'm sorry, but I cannot elaborate... Commented Sep 30, 2015 at 14:32

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Adding "use strict"; to myFunc() seems to work in my tests.
1

You could try to call the method async:

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

1 Comment

Good idea, but this doesn't quite work. setTimeout will run the anonymous function, which will call extFunc. The arguments.callee.caller won't be null. For it to be null, you'd have to do setTimeout(extFunc, 0);.

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.