RMMZ [Solved] A JS question about storing functions in an array

Status
Not open for further replies.

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
JS allows storing functions in an array and calling them later. For me the problems start when those functions are on instances of a class and when I need to refer to the instances via "this.".

I created a test snippet illustrating what I am trying to accomplish, but I clearly lack the syntax knowledge to get me the result since the code errors out on runtime. In the case of the test code, the output should be: "One Two".

Whoever helps with finding a way of storing object instance functions in an array - I am wishing you much great karma. Live long and prosper!

JavaScript:
class test{
    constructor(name){          
        this._name = name
    }
 
    report_name(){
        console.log(this._name)
    }
}

const t1 = new test('One')
const t2 = new test('Two')

var arr = [t1.report_name, t2.report_name]
arr.forEach((x) => {x()})


The context for the question -- in Python I use arrays with functions inside Subscriber class. And I was hoping there is a similarly simple mechanism to store delegates in JS.
 
Last edited:

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
2,343
Reaction score
1,836
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi there!

You can try to use the bind method to store the "this" scope on the functions:

var arr = [ t1.report_name.bind(t1), t2.report_name.bind(t2) ]

Bind Method

Or, you could instead put on the array only the t1 and t2:

JavaScript:
var arr = [t1, t2]
arr.forEach( (x) => { x.report_name() } )
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
@Eliaquim you are a lifesaver, thank you very much!!! Live long and prosper!
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
9,186
Reaction score
5,994
First Language
Dutch
Primarily Uses
RMXP

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

Hi everyone! Enjoying this nice long weekend. How are you all doing? :)
I just successfully tested having events check if specific actors are in a particular party slot and then changing image to match. This will let me make scenes take into account who is in the party at the time and arrange them accordingly - this is important since I'll have extra actors beyond current party. The script used is the same from MV and is compatible with my current core plugin setup in MZ. Thank goodness.
That awkward moment when you're talking about thinking about writing a limited shop stock plugin for MZ and then find out you already wrote one...last May. My memory is so bad.
When I open my project and I'm like, hell yeah.
Some drawings I made in Lapis, one day I intend to involve them in some RPG Maker project.
* maybe two wall frames
*or some sprite flower tiara, or maybe it inspires someone to do something.
bdd19b62e539d17b3b00cc3d63034f7d.jpg

fcca5c8471b34483da8e032cfe579275.jpg

Forum statistics

Threads
118,426
Messages
1,115,868
Members
155,380
Latest member
Jairowind
Top