2

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!

1 Answer 1

1

There's no straightforward way to do that. Instead, you have to implement your function to do so.

Here's a more-or-less simple way, using Array#split() and Array#reduce():

const x = {
    a: function() { console.log ("original function 1") }, 
    b: {
        c: {
            d: function() { console.log ("original function 2") }
        }
    }
}

const setDeep = (object, path, value)=>{
  const pathArray=path.split('.')
  pathArray.slice(0,-1).reduce(Reflect.get,object)[pathArray[pathArray.length-1]]=value
}

setDeep(x, 'b.c.d', function(){console.log('replacement function')}); 

x.b.c.d() //replacement function
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.