function y(fct) {
var a = 2;
var fctStr = String(fct);
var fct1 = eval(fctStr);
console.log("fctStr=" + fctStr); // output: fctStr=function x() { return a + 1 }
console.log("fct1=");
console.log(fct1); // output: undefined. Why it is undefined? I expect fct1 is a function.
return fct1(); // exception: undefined is not a function.
}
function x() { return a + 1 }
y(x) // I expect it returns 3. However, it throws exception at above "return fct1()" statement.
This code in Chrome will get fct1
as undefined
. Why? I expected that fct1
would be a function.
Why this question is asked is because of this: How to write function-y accepting parameter-fct_x which accesses var-a which is required to be defined in function-y?