This is a documentation for Board Game Arena: play board games online !

User talk:SwHawk/Create Modular Code

From Board Game Arena
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

User names

"Tisaac Human Expert" -> Tisaac

"VictoriaLia" -> VictoriaLa

A non-dojo JS approach

Dojo is old and kinda bad. This approach lets you avoid Dojo and this and allows more modern JS.

[X.js]
window.X = {}; // X = your game, or you can use something more generic

...

setup: function (gameData) { // this cannot be an arrow function due to dojo
   X = Object.assign(this, X);
   // the rest of your setup goes here
},
[modules/whatever.js]
X = Object.assign(X, {
  MY_CONSTANT: 1,

  myFunction: (arg1, arg2) => {
    X.someFunction(X.MY_CONSTANT);
    return arg1 + arg2;
  },

  someFunction: (x) => _('The answer is: ') + x,
}

Notes on this approach: - Aside from setup and any framework-override functions, you should be using arrow functions - Always use X instead of this - As usual, you can use any number of modules this way