I'm going to assume that you read my explanations at: Why are object values ββstored inside function calls? .
If you understand what is happening. And you think about it quite difficult. You will see the obvious work.
So, contrary to everything else, I will say: yes, it is possible (within).
The working process
First, a summary. Do you understand that in fact you have two separate links to the same object? Assigning one link will not reassign another link:
var foo = {some : 'values'}; (function (bar) {
So, while the straightforward bar = new_object will not work. Understand that both links point to the same object at the point you are interested in.
This means that although you do not have access to the source link ("foo" in my example, "myObj" in your example), what you have is the object that it points to. This is what we can use.
Provided that you do not care what happens with the current object, and provided that you do not mind that all references to the object have been changed, you can simply deep-delete all its attributes and replace them with a deep copy of the new object.
So, while you cannot do:
obj = model;
You can do:
for (var n in obj) { if (obj.hasOwnProperty(n)) { delete(obj[n]); } } for (var n in model) { if (model.hasOwnProperty(n)) { obj[n] = model[n]; } }
and your code should work as you expect.
If you do this a lot (or just want to make the code more readable), you can encapsulate this logic:
function reassignObjRef (ref, new_obj) { for (var n in ref) { if (ref.hasOwnProperty(n)) { delete(ref[n]); } } for (var n in new_obj) { if (new_obj.hasOwnProperty(n)) { ref[n] = new_obj[n]; } } }
So you can just do:
function watch(event, obj) { PubSub.on(event, function(model) { reassignObjRef(obj,model); } };