Is there any way to make sure that this will always be the same thing when accessed in an object?
I have the following code:
var stack = [];
var callStack = function ()
{
for (var i in stack) {
// does not work
stack[i][0].apply(null, stack[i][1]);
// does not work either
stack[i][0].apply(stack[i][0], stack[i][1]);
}
};
var testThis = {
bar : "something",
show : function()
{
console.log(this.bar, arguments);
}
}
testThis.show('standardCall');
stack.push([testThis.show, ['someVar']]);
stack.push([testThis.show, ['otherVar', 'yetAnotherVar']]);
callStack();
What I want to achieve is this: I want to be able to prepare a stack of functions/methods to be called later (this is just a stripped down example, in reality, the calls will be spread throughout the entire application).
Within the objects, I want to have access to methods and properties of the object just as if they were called "normally" (as shown in the example by testThis.show('standardCall'), which works just as expected), i.e. I need this to always be the same within the method no matter how the method is called. In this example, I want this.bar to always display "something".
Is there any way to ensure this behavior? The call/apply methods do not seem to work.
Note: I'm looking for a universal solution, so, obviously, I can't solve it by referencing "bar" some other way (like testThis.bar), removing it from the object's context etc.)