Say I have a class object named test.
test has various methods, one of them is whatever() .
I have a variable named method = "whatever"
How can I access the method using the variable with test?
Thanks!
Object Methods in JavaScript can be accessed by using functions. Functions in JavaScript are stored as property values. The objects can also be called without using bracket ().
Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.
Simply the dot operator acts as an access provider for objects and classes. The usage of the above operator is as below. It separates a function and variable from an instance variable.
Get the attribute with getattr
:
method = "whatever"
getattr(test, method)
You can also call it:
getattr(test, method)()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With