Is there a way to prevent a function from using global variables like document, window, navigator, and other declared global functions ?
EDIT1: And if I could choose which global objects that are restricted it would be great, because I would like to allow the function to use for an example the Math object and it’s functions…
No, unless…
The only way this task is possible is if the lexical scope of the functions can be altered — that is, the source is modified in some way, such as wrapping it as shown below.
Imagine:
This approach is used often in libraries to create private namespaces but, in those cases, the original source is also available and designed with this in mind. I have used this with
documentbefore to alter a local version of jQuery.While
withmight look promising at first, it is important to realize it is only a lexical construct as well and does not introduce dynamic variables.Happy coding.