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,287
Reaction score
1,782
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,112
Reaction score
5,963
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

Databasing. Just with what I consider "bare minimum", I have listed over 200 skills. That's not all of it, and isn't even considering skills that only 1 class would realistically have. lol

It's a Spanish devlog don't get scared!.
Just read the forum rules just for kicks and rule 14 reminds me of this:
150.jpg
I'm in the process of converting all locks and intimidate/persuade dialogue options into D20 DC based success/fail (modified by player skill). Before, it required a certain flat amount you had to meet, but with rolls, it offers more freedom. For instance, you can repeatedly try to unlock a door instead of just not meeting the requirement to unlock it.
Since we get very close: If you want to enjoy the advent calendar as it is intended, wait til your date and the day in the URL line up, so you have the final day on actual christmas and not the day before... time zones ;3

Forum statistics

Threads
117,050
Messages
1,104,161
Members
153,017
Latest member
sinoth
Top