Aerosys

Veteran
Veteran
Joined
Apr 23, 2019
Messages
583
Reaction score
552
First Language
german
Primarily Uses
RMMZ
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.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,773
Reaction score
2,123
First Language
EN
Primarily Uses
RMMZ
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:
 

Latest Threads

Latest Profile Posts

Doing RPG Maker News for 26th September 2021

Well... it's been a while since I was last here :kaoswt: I don't want to say I'm officially 'back' yet, but I've recently started working with RM again, so I might become active here again :kaohi:
Mmmm I love Coca Cola candy! :kaoluv:
aaaand my power is out. I sometimes think that I'm cursed.

Forum statistics

Threads
115,386
Messages
1,089,489
Members
150,116
Latest member
Kinenveu
Top