In Python, you can implement __call__() for a class such that calling an instance of the class itself executes the __call__() method.
class Example:
def __call__(self):
print("the __call__ method")
e = Example()
e() # "the __call__ method"
Do JavaScript classes have an equivalent method?
Edit
A summary answer incorporating the discussion here:
- Python and JavaScript objects are not similar enough for a true equivalent (prototype vs. class based,
self, vs.this) - The API can be achieved however using proxy or modifying the prototype – and maybe using
bind? - One should generally not do this: it is too far removed from the JS's structure and from the normal use of of JavaScript, potentially creating confusion among other JS devs, your future-self, and could result in idiosyncratic side-effects.