I am developing a JavaScript code instrumentation tool.
Given a piece of code, suppose I need to modify it to finish the following task:
Task: Assigning a new property to each function when it is invoked and access the property from within the function body.
And I am only allowed to add new lines of code into the original code. Other changes like deletion are NOT allowed.
Considering the following example
foo();
function foo() { // All functions are assumed to have names
}
I can modify it by adding two lines of code like this (basically I directly assign and access the added property by referring to the function name):
foo.prop = ...; // give foo a new property
foo();
function foo() {
console.log(foo.prop); // access the property
}
However, if there is another function with same name declared inside foo, the above solution does not work. Considering the following example.
foo();
function foo() { // All functions are assumed to have names
function foo() {...}
}
After modification:
foo.prop = ... // give foo a new property
foo();
function foo() {
console.log(foo.prop); // property does not exist!
function foo() {...}
}
Because of function hoisting, the outer foo is overridden by the inner foo, therefore, I cannot access the added property using function name.
Using arguments.callee may also fail in strict mode. So it is not an option for me. Are there any valid methods that can finish this task? Or it is actually impossible to do?