Define global function

kmll's icon

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.

do.while's icon

Hi
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 .

kmll's icon

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.

do.while's icon

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 ;
Util.func();

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

kmll's icon

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