简体   繁体   中英

javascript - how to access the caller function context object

I have the following situation:

  • a main script with commons methods
  • several other scripts which are using them

 (function() { window.mainMethod = function(param) { //... //console.log("I'm calling this method from context "+myContext.name); [CAN'T DO] console.log(arguments.callee.caller.name); } })(); // Object A (function() { var myContext = {}; myContext.name = "context A"; myContext.fn = function fnA() { window.mainMethod("param A1"); // ... window.mainMethod("param A2"); // ... window.mainMethod("param A3"); // etc... }(); })(); // Object B (function() { var myContext = {}; myContext.name = "context B"; myContext.fn = function fnB() { window.mainMethod("param B1"); // ... window.mainMethod("param B2"); // ... window.mainMethod("param B3"); // etc... }(); })();
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

What I'm trying to do is to log in my mainMethod the context where I'm calling that method from .

First thing I camed up with was to add a parameter to each call :

window.mainMethod("some parameter 1", myContext);

BUT since this is only for log purposes (not functional) and I have thousands of these occorrences to modify, i would like to avoid that.

So I thought maybe there is a way to access to myContext object through the callee.caller property, but this is where I went so far.

window.mainMethod = function(parameter) {
    //console.log("I'm calling mainMethod from context " + myContext.name);
    console.log(arguments.callee.caller.name);
}

This will print out the function name ( fnA ), but still can't access the object where the function is stored ( myContext ).

Any suggestion?


ps: i'm also open for workarounds like, i don't know, binding some hidden property to the function and then retrieve it with arguments.callee.caller.myHiddenProperty ... is it possible?

The closest I think you're going to get is to explicitly change the context using call

 (function(){ window.mainMethod = function(param){ //... //console.log("i'm calling this method from context "+myContext.name); [CAN'T DO] console.log(this.name, arguments.callee.caller.name, param); } })(); // Object A (function(){ var myContext = {}; myContext.name = "context A"; myContext.fn = function fnA(){ window.mainMethod.call(myContext,"param A1"); // ... window.mainMethod.call(myContext,"param A2"); // ... window.mainMethod.call(myContext,"param A3"); // etc... }(); })(); // Object B (function(){ var myContext = {}; myContext.name = "context B"; myContext.fn = function fnB(){ window.mainMethod.call(myContext,"param B1"); // ... window.mainMethod.call(myContext,"param B2"); // ... window.mainMethod.call(myContext,"param B3"); // etc... }(); })();
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you don't want to change the thousands of calls to mainMethod you have, you need to alter the functions that call it ( fn ). The solution is to keep fn as a function instead the result of a function like you have currently, then add the context to it that you can later invoke from inside the mainMethod function :

(function() {
    window.mainMethod = function() {
        // will print the context that has been added to the function object
        console.log(arguments.callee.caller.context);
    }
})();

// Object A
(function() {
    var myContext = { name: 'A' };
    myContext.fn = function fnA() {
        window.mainMethod();
    // notice that I removed the `()` to keep `fn` as a function inside `myContext`
    };
    // now add the context inside the function object
    myContext.fn.context = myContext;
    // then just call it
    myContext.fn();
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM