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

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

From Board Game Arena
Jump to navigation Jump to search
(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...")
 
Line 1: Line 1:
 
==User names==
 
==User names==
 
"Tisaac Human Expert" -> Tisaac
 
"Tisaac Human Expert" -> Tisaac
 +
 
"VictoriaLia" -> VictoriaLa
 
"VictoriaLia" -> VictoriaLa
  

Revision as of 18: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