I have an object containing functions that are nested potentially deeply (could be more levels than this so I need a solution that will work at any depth):
x = {
a: function() { console.log ("original function 1") },
b: {
c: {
d: function() { console.log ("original function 2") }
}
}
}
I want to be able to pass the function's keypath as an argument to another function and replace the original function with a new one:
g = function( functionAddress ) {
functionAddress = function() { console.log( "replacement function" )}
}
so that after executing:
g("x.b.c.d");
x.b.c.d() // replacement function
g("x.a");
x(a) // replacement function
I've been able to process the argument using split, shift and reduce so I can access the function at the specified address as in:
keypath = x["b"]["c"]["d"]
but if I try to use newFunc to replace the function at that location, it only replaces the value of keypath, not the function at that address.
Any help would be appreciated. Thanks!