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

User talk:SwHawk/Create Modular Code

From Board Game Arena
Revision as of 17:08, 14 June 2022 by Mogri (talk | contribs) (Created page with "==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 <tt>th...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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