0

Is there a way to get all method calls on an object to be passed to a function that gets the function name as a string and the arguments as an array passed to it.

E.g, in pseudo code:

function DynamicFunctions() {
    this.tryCall = function(functionName, arguments) {
        if(functionName == "dynamic1") { console.log( "Function dynamic1" )
        else { console.log("A different function"); 
}

The usage is:

var obj = new DynamicFunctions();
obj.dynamic1();
obj.somethingElse();

The output in that case would be:

Function dynamic1
A different function
Max Meijer
  • 1,530
  • 1
  • 14
  • 23
VitalyB
  • 12,397
  • 9
  • 72
  • 94
  • 3
    It seems you want [ES6 proxies](http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies). But it also seems obvious that you're facing a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Denys Séguret Jun 09 '14 at 12:47
  • Basically it would allow you to create objects whose behavior can be defined in a more dynamic away. For example, you can have an object that loads its available functions from a JSON file or create a dynamic proxy for another object functionality. – VitalyB Jun 09 '14 at 12:53
  • I guess I am just trying to find out if there is a mechanism in Javascript similar to [TryInvokeMember in C#](http://proq.blogspot.co.il/2011/06/more-on-dynamicobject-tryinvokemember.html). I.e - A way to handle calls to unexisting properties/functions internally. – VitalyB Jun 09 '14 at 12:55
  • 2
    The duplicate question has your answer. – Tomalak Jun 09 '14 at 12:56

0 Answers0