- Joined
- Jul 20, 2021
- Messages
- 74
- Reaction score
- 39
- 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!
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.
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: