Forums > Javascript

Define global function

Feb 01 2014 | 3:06 pm

Reading this article a global variable can be defined like this:
// Global (Max namespace) variables
glob = new Global("bounce");
glob.starttime = 500;

Is there some way to define a global function? I have a few utility functions that I would prefer to maintain just in one place and not in every patcher.

Feb 01 2014 | 4:36 pm

As u may have noticed that our "global" is an object . We can pass functions within . Just make sure about what u want to return and to were "this" is pointing to . Your function will not be global to js instance so " this" will point to your global object .
u will need to take care for it if u will be in need to pass references to "this" . As i dont know how u want to use it , im just warning in case of some surprises .

Feb 02 2014 | 8:04 am

Ok thanks for the advise. Can I please ask you if you can show an example how I can define a function using this and I can then test it.

Feb 02 2014 | 8:22 am

Hi !

inside one instance :

var Util = new Global("util");
Util.echo = 9 ;
Util.func = function(){
 post("i think im allowed to do this\n");
 post("got echo : " + this.echo +"\n");

inside another instance :

var Util = new Global("util");
Util.echo = -10 ;

i believe that it should work this way . let me know

Feb 02 2014 | 12:00 pm

Brilliant – thanks! This seems to work just the way I wanted:)

Viewing 5 posts - 1 through 5 (of 5 total)

Forums > Javascript