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,764
Reaction score
2,114
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 Posts

Latest Profile Posts

Is it weird that i feel overwhelmed from reading codes that i wrote few months ago? Lol.

I dont even know where to continue!:hsad:
Ami
Gosh,I want to Play this Game Again one more again!

Seeing my Own Character & Kannono Once Again!
Tales_of_the_World_Radiant_Mythology_3_JPN_PSP-Coverart.jpg
I've been a bit to depressed to get work done on the game lately. Don't worry, I'll be okay.
LunarFlareStudios wrote on Solar_Flare's profile.
Hey, my name is Lunar Flare Studios, and you're Solar Flare? That's so weird!

Forum statistics

Threads
115,301
Messages
1,088,745
Members
149,943
Latest member
Chymora
Top