Define global function

Feb 1, 2014 at 3:06pm

Define global function

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.

#279819
Feb 1, 2014 at 4:36pm

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 .

#279825
Feb 2, 2014 at 8:04am

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.

#279858
Feb 2, 2014 at 8:22am

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

#279862
Feb 2, 2014 at 12:00pm

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

#279884

You must be logged in to reply to this topic.