i was trying to use the javascript APPLY function to pass scope from 1 function to another, but it seems that i might be doing it wrong?
Here is a fiddle if you need it: http://jsfiddle.net/C8APz/
Here is my code:
function a(){
var x = "hello";
b.apply(this, []);
}
function b(){
console.log("x: ", x);
}
a();
i was thinking that while the scope is passed, the variables / variable reference are not.
Is there a way to do something like this without defining Globals?
Should i add the data to the actual part of it, such as this.x = x;
? and then in the other function just fetch it? var x = this.x;
function a(){
var x = "hello";
this.x = x;
b.apply(this, []);
}
function b(){
var x = this.x;
console.log("x: ", x);
}
a();
Edit: It seems that the second example assigns in the global scope, which isnt good, and with the scope, i was attempting to pass an understanding of context to. It seems that you really have to define a context before you pass it, otherwise, for the most part this refers to window