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?