In a an application that contains different views that use different JavaScript visualizations, I am experiencing that the JS includes bite each other due to Vaadin’s ‘single page’ concept.
E.g. Both visualisation JS includes define var height, width. The first time one of these pages gets loaded, this goes well. The second time another page gets loaded, I get a nice SyntaxError: Identifier 'height' has already been declared, obviously since these variables are defined in the same global scope.
Is there a way to isolate JavaScript files’ scope to only the view they are included in…?
Of course, I could prefix all variables to make them unique, but I would rather not do that.
Are these JS files written by you or somebody else? JavaScript modules are automatically scoped to the module so they will not leak variables into the global scope. Instead you can choose what, if anything, you put into window.*.
Artur Signell:
Are these JS files written by you or somebody else? JavaScript modules are automatically scoped to the module so they will not leak variables into the global scope. Instead you can choose what, if anything, you put into window.*.