0

My main goal here is to pass a variable by reference. I have created a function that creates a buffer like this:

function createBuffer(buffer=null){
    var _buff = Object.assign({}, {buff: buffer});
    return buffer;
}

And I am currently using this buffer inside functions like this:

function myFunc(buff){
    console.log(buff.buff);
    buff.buff = "Hello World";
    return;
}

Now I would like to ask if I could somehow define a getter/setter for the buffer object itself, so that I could just do this:

function myFunc(buff){
    console.log(buff);
    buff = "Hello World";
}

Is this possible? Or what would be the best way to pass variables by reference (besides from the one I am currently using) ?

chris
  • 3
  • 1
  • 1
    The answer is no. – Pointy Apr 15 '20 at 03:53
  • Welcome to SO! I'm not sure what you're asking, but `buff` isn't actually the object. It's a local variable that happens to point to the object. So when you do `buff = foo` it just reassigns the local variable and can't mutate the object. Long story short, I don't think you can do what you're trying to do (and that's probably a good thing). – ggorlen Apr 15 '20 at 03:54

0 Answers0