Question

Given a Javascript function, is it possible to verify that the function has no side effects; i.e., that the function does not change the value of any variables that are declared outside the function's scope?

Was it helpful?

Solution

You would need to write an effect analysis for JavaScript, which would require an effect semantics for JavaScript. Some of the things to consider:

  • does the code access primitive operations that have side effects? (e.g. memory writes, disk writes, file IO, updating any global state?)
  • if the code does indeed write to variables, do they escape local scope? (i.e. is it in the ST or IO monad?)
  • is any communication done via shared variables?

People have written type systems for impure languages to statically determine if side effects are present. Ben Lippmeier's thesis covers a lot of the ground.

OTHER TIPS

If it accesses any variables that is not defined within the function yes of course it affects any global variables that may have the same name.

You can look at JSLint and ADSafe and run your file against those tools.

http://www.jslint.com/

http://www.adsafe.org/

HTH.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top