For example the javascript code is:
function a() {
this.Foo = function() {//...}
}
What I usually do is that I create a global reference:
_a = new a();
Then use it wherever I want in body or other script area:
_a.Foo()
Is this good or bad practice? or is there a better/more professional way to do that?
In general Javascript developers try to avoid this, because of the possibility of different libraries conflicting with each other in the global scope. To avoid this, consider (if possible) using what’s called a closure to create a private scope for your variable. That looks like this:
Everything declared inside the inner function is invisible to code outside it.
If you can’t use a closure for whatever reason, then an alternative would be to create a “namespace” for your variables.
This way you are able to restrict your code to acquiring a single global name (MyCompany), with other variables and methods residing inside that namespace.