3

Is it possible to modify all references to an anonymous function so that invocations from other objects reflects the changes?

Below is an example of a situation where obj1.name and obj2.name both points to the same function, but since the function is passed by value when creating obj2.name = obj1.name, changes in obj1.name doesn't affect obj2.name.

In this example, I would like to modify that function so that it returns 'edited' for all objects.

//This two declarations are part of the original code that I cannot modify.
let obj1 = {
    name: function() { return 'original'; }
}
let obj2 = {
    name: obj1.name
}
// obj1.name() >> 'original'
// obj2.name() >> 'original'

// At this point, I want to modify the returned value.
obj1.name = function() { return 'edited'; }
// obj1.name() >> 'edited'
// obj2.name() >> 'original'
Bruno García
  • 177
  • 11
  • No. Having it as part of multiple objects is a bad design (as you've discovered) and you should change it rather than try to come up with a hacky solution. – Adam Jenkins Jun 17 '20 at 01:07
  • 2
    I'm confused. You said *"This two declarations are part of the original code that I cannot modify"*, but you are modifying them here: `obj1.name = function() { return 'edited'; }`. So, what are the rules? If you can change `obj1.name` you surely can change `obj2.name`, as suggested in the current answer. – Gerardo Furtado Jun 17 '20 at 01:08
  • Maybe https://stackoverflow.com/q/2136522 will be helpful? – ATOMP Jun 17 '20 at 01:11
  • @GerardoFurtado I'm trying to simplify a more complex situation where I have several objects pointing to an anonymous function. I want to modify that function for all the objects that might call it (I have no idea how many objects might be calling that function, so I have to directly attack the function) – Bruno García Jun 17 '20 at 01:34
  • If the objects already exists, and they each contains their own anonymous function, then by definition there's no way you can change what those functions do unless you can enumerate over that list of objects. – kmoser Jun 17 '20 at 02:59

2 Answers2

1

Try this. That way obj2.name is a function also.

If you wanted obj2.name to be a property, then after obj1.name was changed you would need something to watch that and upddate obj2.name. So its probably easier to use a method.

let obj1 = {
    name: function() { return 'original'; }
}
let obj2 = {
    name: function() { return obj1.name() }
}
lfalin
  • 4,219
  • 5
  • 31
  • 57
Max Carroll
  • 4,441
  • 2
  • 31
  • 31
  • I didn't explain myself enough, the declaration of both obj1 and obj2 is part of the original code, I want to modify the function having that specific declaration. – Bruno García Jun 17 '20 at 01:05
  • ahh do you mean like this const someFunction = () => {} let obj1 = { name : someFunction} let ob2 = { name : someFunction } someFunction = () => { console.log("do something else"} – Max Carroll Jun 17 '20 at 01:08
  • Yes exactly! The problem is that my function is anonymous so I cannot (or don't know how) modify it directly – Bruno García Jun 17 '20 at 01:13
0

Okay I got it, each time obj1.name() is invoked, it then gets which ever version of someFunction is current at the time, as opposed to assigning once and never checking again


let someFunction = () => "original"

let obj1 = {
  name:  () => someFunction()
}

let obj2 = {
  name:  () => someFunction()
}

console.log(obj1.name())
console.log(obj2.name())

someFunction = () => "edited"

console.log(obj1.name())
console.log(obj2.name())



Max Carroll
  • 4,441
  • 2
  • 31
  • 31
  • Okay got it, the additional function which returned a function was not at all necessary – Max Carroll Jun 17 '20 at 01:25
  • Thanks for your help, that's a good idea but still I can't modify the function that way because in my case the function doesn't have a name (function(){...) – Bruno García Jun 17 '20 at 01:35