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

User talk:SwHawk/Create Modular Code: Difference between revisions

From Board Game Arena
Jump to navigation Jump to search
Line 34: Line 34:


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

Revision as of 17:09, 14 June 2022

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