MV How to.. Named Parameters?

Hey all,

I'm currently practicing Named Parameters. At the moment my code looks like this:

JavaScript:
drawPath({
    start = "south",
    end = "north",
    mapName = "Path",
    weightFunction = undefined,
    thickness = 3,
    mode = "smooth"
} = { }) {

So I can call it by e.g.

Code:
drawPath({ start: "west", end: "east", thickness = 4 })

or

Code:
drawPath()

I'm just wondering if this is the right method to do it, as the method signature with {...} = { } looks a bit weird IMO. By the way, I want to have my code to be compatible with MV.
This uses two concepts:
  • Destructuring assignment (function arguments { arg1, arg2, arg3 })
  • Default parameters (= {})
It's important to use default parameters because otherwise passing in a non-destructurable value like undefined will cause an error when it attempts to get the parameter values:
  • Uncaught TypeError: Cannot destructure property `arg1` of 'undefined' or 'null'.

Both of these concepts were introduced in ES6. At some point (v1.6.0?) RMMV updated its playtest & deployment apps from ES5 to ES6, so this will work with the latest versions of RMMV. If you want to support older versions then you may have to change your approach a bit. In that case you might find this post helpful:

Original thread: https://forums.rpgmakerweb.com/threads/how-to-named-parameters.139104/