javascript - Function to modify object in arguments, not just a property -
i'm sure has exist somewhere haven't been able find it...
i'm trying write function takes object argument , updates reference. not property of reference, , not reassign object, update whole reference.
note pubsub in there demonstrate need asynchronicity , flexibility in types of objects passed in , updated.
best explained example:
//ideally how function work function watch(event, obj) { pubsub.on(event, function(model) { // want update entire object // understand currently, reassigning // know can obj.prop = model, that's not want obj = model; } }; //example usage var myobj = {"name" : "tom"}; watch("anevent", myobj); console.log(myobj.name); //still "tom" // time passes, somewhere pubsub.trigger("anevent", {"name" : "john"}) console.log(myobj.name); // should read "john"
is sort of scenario bind()
method, e.g. $.bind()
or currying useful, or using "this" in appropriate context?
it's not possible.
what confuses in js runtime don't have way manage actual reference values, can assign reference value variable.
that's - can "attach" reference given variable name, cannot modify reference value.
to clear said above: memory object allocated on initialization
var o = {};
after reference unnamed object (objects don't have names, variables do) assigned o
variable. after cannot re-allocate object same place in memory.
Comments
Post a Comment