function x() {
return a + 1; // it can be anything accessing var-a.
}
function y(fct_x) {
var a = 2;
fct_x(); // exception: a is not defined
}
y(x); // exception: see above
// x is any function which will access var-a which is required to be defined in function-y
Question: how to write a function-y like above so that calling fct_x() within function-y will not throw exception?
Note: fct_x is any function (user given) which accesses var-a. The var-a not defined in function-x, but is required to be defined in function-y.
With referring to Is it possible to achieve dynamic scoping in JavaScript without resorting to eval?, I have tried like this, but it does not work.
Why I ask above question: This question comes from "MongoDB mapReduce", like this: There are 3 properties and 28 functions available for the map-reduction. One of the 28 function is emit(key,value). (See MongoDB doc).
Take this example,
function mapFunction() { emit(this.fieldA, this.fieldB) };
db.collection.mapReduce(mapFunction, reduceFunction, {...}); // calling mapReduce-function
The emit-function within mapFunction is not defined in mapFunction. It is "provided"/"defined" somewhere within function db.collection.mapReduce. How function-db.collection.mapReduce is written to be able to proivde such emit-function for a user-given-mapFunction to call?
[var a] equivalent to [function-emit]
[function y] equivalent to [function-mapReduce]
[function x] equivalent to [function-mapFunction]