Is there a way to determine if a JavaScript function has side effects?

Given the Javascript function, is it possible to verify that the function has no side effects; that is, that the function does not change the value of any variables declared outside the scope of the function?

+4
source share
2 answers

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

  • Does the code support primitive operations that have side effects? (e.g. writing to memory, writing to disk, IO file, updating any global state?)
  • If the code is actually written to variables, do they avoid the local scope? (i.e. is in the monad ST or IO?)
  • Is this any connection made using 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 space.

+3
source

If it refers to any variables that are not defined in 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 the file with these tools.

http://www.jslint.com/

http://www.adsafe.org/

NTN.

0
source

Source: https://habr.com/ru/post/1413273/


All Articles