[JS] Global variables
-
We all know that $ Global variables are bad in Ruby. And that you have to wrap your code inside a module, (doublewrapping is even better) not to let your code crap(expression from Dan
) on other peoples code.
So on the JS side of things. When the dialog gets more complex there comes a point where one have to store an array or such outside of functions. Simply slapping arguments around wont work. (one can use hidden inputfields, but thats not the question here)
I can imagine a lot of newcomers are not always aware that the var becomes global then. If I understand it right is the same as in ruby $ ?
Read some here..
http://www.rafilabs.com/javascript-namespaces-jquery-noconflict-and-that-pesky/
So how do we deal with Webdialogs namespacing?
Are there any special circumstances since we are inside Sketchup enviroment?
Something like, global vars are not that harmful if kept at minimum?I see Thomthom is doing a namespacingfunction in TT_lib, so that got me thinking.
-
@jolran said:
:circle: So how do we deal with Webdialogs namespacing?
Same as regular JavaScript namespacing.
@jolran said:
:circle: Are there any special circumstances since we are inside Sketchup enviroment?
No.
@jolran said:
:circle: Something like, global vars are not that harmful if kept at minimum?
Globals should be avoided regardless.
In the case of WebDialogs and JS, one WebDialog instance won't affect another instance. All is restricted to the scope of the HTML page loaded.Namespacing is still good when you want to use other libraries, you then want to ensure you do not conflict.
I have to admit I'm a little bit more relaxed when it comes to JS in WebDialog, I often don't namespace them if I just make a simple dialog. (I loathe the JS syntax for namespacing - it's messy to say the least!) But as of TT_Lib, which is a library I have namespaced it because it's mean to be used in different environments.
It is good practice to always namespace and isolate your code.
-
Thank you Thomthom. I think you answered all my questions.
@unknownuser said:
All is restricted to the scope of the HTML page loaded
This was my main concern. I don't want to pollute other people's plugins if I create a global var.
@unknownuser said:
Namespacing is still good when you want to use other libraries, you then want to ensure you do not conflict.
Ah, I think I understand now. Like if you create a Jquery plugin for public release, for ex?
@unknownuser said:
But as of TT_Lib, which is a library I have namespaced it because it's mean to be used in different environments.
A, yeah. I forgot for a minute it was a library when I was pooking around. Your library is a very useful resource for me, since you are using Jquery
.
Advertisement