I got a quiz about function writing like this:
/* ====== can not modify this line below till next dividing line ========= */
function foo() {
var obj = {
x : 1,
y : 2,
bar : bar()
}
function bar() {
/* ====== can not modify this line above ================================= */
/* so how can I get obj.x and obj.y here and returns their sum ..? */
/* ====== can not modify this line below till next dividing line ========= */
}
return obj;
}
console.log( foo().bar ); // expected 3
/* ====== can not modify this line above ================================= */
I found two ways myself, one is get foo.toString()
and do some REGEX
magic.
the other is register a global variable like window.run
to control foo()
runs only once.
However I am wondering is there any other ways can solve this?
Thank you for your reply~