Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Moving function out of on()

I find the JavaScript examples on the wiki to be a little too terse to be easily understood. I'd like to break up my own scripts into easier to manage chunks. One thing I'd like to do is to take the function part of an on() statement and declare it separately. For instance, this: on("chat:message", function(msg) {    // Stuff to do }); becomes more like this: function myFunction(msg) {    //Stuff to do } on("chat:message", myFunction(msg)); Except that's not the right syntax. How can I do this?
1470667407

Edited 1470667732
Lithl
Pro
Sheet Author
API Scripter
The correct syntax would be: function myFunction(msg) { // ... } on('chat:message', myFunction); When you only supply the function name like this, it's a reference to the function. When you use myFunction(msg) as in your example, it's actually calling  the function, and the return value is what's passed to on. If your function returned a function object that would be fine, but that's not generally the case. If you look at scripts on the GitHub repo, this is common for the ones which utilize the  Revealing Module Pattern (Aaron and I both use it extensively in our scripts). They often take a form similar to: var myNamespace = myNamespace || {}; myNamespace.mySript = (function() { 'use strict'; var version = 1.0; function handleInput(msg) { //... } function checkInstall() { //... } function registerEventHandlers() { on('chat:message', handleInput); } return { registerEventHandlers: registerEventHandlers, checkInstall: checkInstall }; }); on('ready', function() { 'use strict'; myNamespace.myScript.checkInstall(); myNamespace.myScript.registerEventHandlers(); });
Huh. I could've sworn I tried that. I knew the syntax I showed was calling the function instead of referencing the function, but I couldn't find a way to reference it. But now I just tried it as you show and it works. Oh well, thanks!
1470669624
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Doing it this way also keeps the pollution of the global namespace down (as Aaron is very fond of reminding us ;) ).
1470671171
The Aaron
Pro
API Scripter
SCOPING!!!!!! =D   Totaly agree.  5/5, would buy again. In all seriousness, some earlier scripts had unfortunate side effects, particularly ones developed by the same person.  I remember one case where a set of scripts only worked if they were a) both installed and b) installed in the right order, as the processing of events by one set several global variables that another happened to use (but NOT by design).  Tracking down the problems when only the second script was installed took quite a while and included using google to search for some variable names in the API forum (this was before there was a search for forums) to figure out what other script populated them (this was before the official repo as well).