I'm trying to get the example code below to work.
Edit (Trying to be more clear what the aim is):
I wan't to make all the functions and variables of the obj
available in the functions setup
and draw
as they where globals. This is not for the browser. This is for Adobe ExtendScript so I can only use EcmaScript 3 and some polyfills. The lib.js
file is provided by me and gets included before the the user-script.js
file. The user has a reference what functions are availble and might use them within setup
and draw
. This is actually pretty similar to what P5.js does but I'm are trying to achieve this for InDesign. We can of course call the obj.foo()
. The aim is though to get rid of the obj.
to give the user the possibility to just call foo
and get the result of obj.foo
.
This is lib.js. It is just a small part of the library to illustrate what I have at hand.
var obj = {
foo: function() {
console.log('foo');
},
bah: function() {
console.log('bah');
},
foobah:function(arg){
return arg*2;
},
CONST:"Hello World",
go:function(){
// looks in the global scope if there is
// a setup and draw function and should patch them.
if(typeof glob.setup === 'function'){
glob.setup();
},
if(typeof glob.draw === 'function'){
glob.draw();
}
}
};
This could be the user-script.js. The structure we Provide is:
- #include for the lib
- function setup(){}
- function draw(){}
- obj.go() as execution of everything (might be removed later on)
I can not tell the user to write more additional code into setup and draw. The part for the user should be so reduced he can write this by hand and does not need to use a boilerplate or something like this.
#include lib.js
function setup() {
foo(); // might be called or not
console.log('in setup');
}
function draw() {
bah();// might be called or not
console.log('in draw');
}
obj.go()
Thanks to all your answers. I will review them and get back to report what was the final decision. All of them seem to solve the problem in different ways. I currently can't tell which is the "right" answer. Those we seemed be nearest to my question got an upvote from me.