Skip to content
Advertisement

Define and use class methods from variable

Let’s say I have two classes with some methods.

First class:

JavaScript

Second class:

JavaScript

So my question is two fold. Say in my main I have something like:

JavaScript

Then I just call MyFirstClass with the y1 and y2 lists and get an output.

But as can be seen in the beginning of main I have FUNCTION and NORM. Is there any way to call MyFirstClass from there, and then reuse it all through main, i.e. something like:

JavaScript

This doesn’t work obviously. So how can one do that ?

Also, and this is probably a build upon the above, how can I, once again, choose the norm function to be used in the other class ? For instance, if I update MyFirstClass a bit to:

JavaScript

And then when the class is called it takes the NORM argument from the main function, i.e. something like:

JavaScript

I have no idea if this is even possible without making the code “uglier”.

Advertisement

Answer

You need to create an instance of MyFirstClass, and pass it as the self argument to the function.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement