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,350
Reaction score
1,842
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,198
Reaction score
5,997
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

I have been enjoying a game on my iPhone, called Wonderbox. It is just a little game where you create adventures, share them, whilst also being able to play others. It's a bit zelda-esque. I have started some youtube for those, gonna start with RPG maker things soon as well. So if you wish to see, do check it out :)
SF_People3_3 added!
index.php

ScreenShot_1_3_2022_1_32_12.png
I feel QSprite for MV is underrated. I basically am able to use my chibi sideview battlers I drew on the maps and have all the motions available. this'll help a lot with cutscenes.
I've figured out how to reasonably tall-ify chibi sprites on my editor. I like that making tall sprites gives me control on expressing how tall characters seem on the field relative to each other as well as better differentiate adult sprites from child sprites.
doing some wacky experimental stuff in MV right now :kaoluv: I cannot wait to drop what I've been working on

Forum statistics

Threads
118,562
Messages
1,116,961
Members
155,606
Latest member
Kage2048
Top