find referenced object's attributes in an unknown function
<p>I've a quite tricky problem to solve.
I use a particular js library where sometimes I need to scan user-defined functions that reference particular object's attribute (something like specialObject.attribute). In order to use those attributes it's always used a get function, so the code will be something like this</p>

<pre><code>myFunction() {
    return myObject1.attributeX.get() + myObject2.attributeX.get();
}
</code></pre>

<p>I need to "scan" in some way that function, and save a reference to object.attribute in an array (ok, I know it looks crazy but the library is crazy too and I'm forced to work with it...).</p>

<p>What I do is parsing the function to a string, analyze that string to find any object.attribute.get() maching and saving a reference to window[object.attribute] in an array. Great, it works fine! But...
The real problem is when objects are in array, like in this example</p>

<pre><code>myFunction{
    var toReturn = 0;
    for(var element in arrayOfObject) {
        toReturn += arrayOfObject[element].attributeX.get();
    }
    return toReturn;
}
</code></pre>

<p>How can I save a reference to used attributes? I was wondering if is there a way to duplicate the function, modify it by adding a line of code and then executing it so I could transform the function in something like</p>

<pre><code>myFunction{
    var toReturn = 0;
    for(var element in arrayOfObject) {
        toReturn += arrayOfObject[element].attributeX.get();
        usedAttributes.push(arrayOfObject[element].attributeX); // added line
    }
    return toReturn;
}
</code></pre>

<p>Is it possible? How can I do? Is there a way to solve this problem in a better way? (consider that I can only modify the library, I don't know nothing else about function that users will write and I can't force users to add that code of line themselves)</p>
