2

I'm wanting to directly update the object referred by a function argument from within the scope of that function. An example:

var thisObj = { "val" : "original value" }; 
function modFunc(objRef) {
    objRef = { "val" : "modified" };
    console.log(objRef);  // { "val" : "modified" }
}
console.log(thisObj);  // { "val" : "original value" };

I understand why this happens, and I realize that I could have used

    objRef.val = "modified";

within modFunc to perform the modification. For reasons specific to my project though, I would like to be able to accomplish something like:

function modFunc(objRef) {
    objRef.self = { "val" : "modified" };  // Of course this will not work, 
    // but is there an Object property or method that allows one to access 
    // the actual memory pointer being referenced by the variable?
}

where I can specify that I want to directly modify thisObj to reference the new object that I instantiate within modFunc. Is this possible? I know there are tools like Object.assign() that I could use to do this but it's not supported universally, or library tools like bind() that I could use if I imported the library, but it just seems like there might be some native syntax that would allow me to do this and I just haven't been able to find it?..

I've spent some time looking, otherwise I try not to ask questions like this. I also know there are other similar posts to this one, but nothing exactly like what I'm asking and I don't have the 'reputation' here to be able to respond directly to posts etc. so I didn't see an easy way to focus the discussion. Anyway, thanks for any help!

3 Answers 3

2

What you're asking for is, essentially, a call-by-reference mechanism, or the ability to explicitly create a reference to a variable. In JavaScript, that's not possible. You can (as you note) pass around references to objects and use those to modify object property values (and to add and remove properties, even), but that's not quite the same thing.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok thank you, this is exactly what I was asking. I'd just hoped there was some way to do it that's a little more straightforward than the workarounds I've been considering. Figured I'd put it out there and see if anyone had a direct solution. Thanks for your reply!
There is an obscure corner case involving the arguments object that allows a call-by-reference mechanism, but it's not useful here.
@Pointy - yeah, things can get pretty damn off-track there! :-)
@ScottSauyet Wow, thanks for the link. Definitely some interesting reading.
@jtrick - Interesting that it exists. I don't think there are many -- if any! -- useful exploits of it. But it's always fun to know about these corner cases.
1

This would be true assignment by reference which javascript doesn't support. It only supports the false, misunderstood kind of assignment by reference.

1 Comment

Ok thanks. I guess I'll just have to work in a solution that's a little less elegant than what I'd hoped for.
0

since you cant deal with references, you can simply make objRef a global variable, and not pass it as an arg at all. then you're updating the original object.

1 Comment

I appreciate the feedback, although in this case I'm needing to protect the memory location from potential outside access. I'd originally had the entire object construct encapsulated by a closure and was acting on 'thisObj' directly from within 'modFunc' (Essentially the same mechanism you're referring to). In a different situation, your suggestion would work great though; thanks for the help.

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.