I’m trying to understand javascript scopes, and I want to know if in the following example:
// B can access 'context'
var a = function (context) {
b = function () {
console.log(context);
};
b();
};
a('nicccce'); // works!
// C can't access 'context'
var d = function () {
console.log(context);
};
var c = function (context) {
d();
};
c('oh oh'); // breaks
is there a way of accessing ‘context’ from the ‘d’ function?
EDIT:
I know I could pass the argument to the d function and thats obviously the sane thing to do, but I wanted to know if there was another way of doing it, maybe using this.callee as this.callee.arguments[0].
You can either
1) pass
contextintodas you do forcanda. or2) put
contextint the lexical scope ofd.Option 2 sounds complicated, but it isn’t. That is exactly what you are doing in the case of
b.contextis “closed-in” to the execution scope ofbbecause the variable is available on the scope of the caller ofb(because it is an argument toa).