S
S
Spooky 20202021-07-28 19:04:27
JavaScript
Spooky 2020, 2021-07-28 19:04:27

How to bind an object to a function to process it?

There is a function that performs manipulations with a certain type of objects from a certain collection.
First, I pass a constructor class and a function to the "factory" to work with instances
of this class. Then, when creating an instance, it needs to know which function is for its type,
since the collection will contain objects of different types that other functions are designed to work with.

You need something like a function reference (like in C or C++). I don't really want to use the similarity of parallel arrays.
Perhaps it is worth wrapping the function in a separate object and assigning a reference to it already? Does it work like this in JS?
You cannot write to the prototype - the function must be associated only with collection objects, and not with all instances.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-07-28
@Stalker_RED

In js, you can pass a reference to a function.
It's not entirely clear what exactly you want to do, but perhaps WeakMap will suit you, where your objects will be the keys, and the function values.
Or pass a set of functions to your factory, and teach it to apply the necessary one depending on the type.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question