In PHP every class contains a "magic" __call function. Using this one can dynamically intercept all calls to a class. For example using
class TestClass {
public function __call($functionname, $arguments) {
.. functionname called
}
}
See http://www.php.net/manual/en/language.oop5.overloading.php#object.call
Is something similar possible in JavaScript/Node.js? Either on a module (loaded by require) or for classes?
Update: Thank you for all who commented. This does not seem to be possible in pure JavaScript. At least currently.
- As bfavaretto pointed out, this can be done using enabling harmony proxies in Node.js. See Enable Harmony Proxies in nodejs.
- This question seems to be a duplicate of Get notification when javascript object is invoked.
- Possibly related:
__noSuchMethod__
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod), which is marked nonstandard.