0

I have a big object defined in the global scope called global. I would like to dynamically find all the referenced properties under my variable global. That is, all the properties that were accessed during the execution of the code.

I want to do static code analysis to extract all the referenced properties under my variable. I can search for these patterns: global.PROPERTY_NAME AND global[PROPERTY_NAM]. However, what about the complicated cases like these ones

var tmp="PROPERTY_NAME";
global[tmp]

OR

var tmp=global;
tmp.PROPERTY_NAME

and the other ones?

I don't want to get all the variable's properties. I only want a list of the referenced ONES!! the properties that were referenced in my source code only

1
  • Perhaps a little "for" loop, like : for(element in global) { alert(global[element]); } Commented Mar 1, 2013 at 14:35

2 Answers 2

3

After your edit:

What you're looking for is JavaScript Proxy objects. Here is a tutorial on how to do this using them.

Proxy objects let you wrap an object and execute a method whenever its properties are accessed. Unfortunately as it currently stands they are not widely supported.

This is currently only way in JavaScript to accomplish this without changing your original global object.

You can turn them on in Chrome by enabling experimental JavaScript in the about:flags tab.

Before your edit:

The feature you're looking for is called reflection, JavaScript supports it well and natively

Here is some code that iterates through an object and gets its properties

for(var prop in global){
    if(global.hasOwnProperty(prop)){ //this is to only get its properties and not its prototype's
        alert(prop+" => "+global[prop]);
    }
}

This is fairly cross-browser. More modern browsers allow you to do this in simpler ways like Object.keys(global) which returns an array containing all its enumerable properties, or Object.getOwnPropertyNames(global) which returns both enumerable and not-enumerable properties.

Sign up to request clarification or add additional context in comments.

3 Comments

I dont want to get all the variable properties. I only want a list of the USED ONES!! the properties that were mentioned in my code only
@nullException First, there is no need to be rude, I'm helping you out of my own free time and for no return. Second, I'm not sure what you mean by 'USED ONES!!', do you mean referenced ones? Ones that were assigned into? Ones that were referenced? Ones that are functions and were called?
Thanks for your help. a list of the referenced properties
1

Due to the dynamic nature of JavaScript you won't achieve that with static code analysis. Think about cases like this:

var prop = document.getElementById('prop').value;
global[prop];

Impossible. The alternative, dynamic analysis, would mean that you modify your global object to log access to its properties, then run the code. This is easily possible in JavaScript but it won't help you either because how would you assure that you have covered every possible access? Especially in a 5 MB JavaScript, there are most likely edge cases that you will oversee.

So, if you can't narrow down your requirement, it won't be possible.

5 Comments

It actually is possible, just not supported cross-browser. See my answer and kevinwestern.org/blog/2012/11/07/… for example.
Proxies do exactly what I described and have exactly the limitations that I mentioned.
"dynamic analysis, would mean that you modify your global object" - It does not require changing to global object using ECMAScript 6 proxies, just proxying it, that's the point, you don't have to cover edge cases. I'm really excited about proxies, they offer a lot of power :)
OK you don't have to modify the object but that's not the problem. The problem is how to run the code to cover every possible access to the ojbect.
It's actually pretty awesome :) If you look at the first example "logging access" here: kevinwestern.org/blog/2012/11/07/… it's a matter of 13 lines of code. I've previousloy asked this question stackoverflow.com/questions/11144589/… which is another place where proxies might be awesome :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.