0

I've seen lots of questions about passing objects by reference in Javascript, but not the object and properties by reference. Is it possible?

Right now I only found a way to do it by going through some type of logic like this, which is terribly inconvenient:

let multipliers = {
    none:1,
    sin:2,
    cos:3,
    tan:4,
    atan:5,
}

incMultiplier(shapesMovements[index], "rotation", "x", "sin")

function incMultiplier(shapeMovement, kind, dimension, multiplier){

    var numOfKeys = Object.keys(multipliers).length;
    
    if(kind === "rotation"){
    
        if(dimension === "x"){
        
            if(multiplier === "sin"){
                if(shapeMovement.rotation.x.multiplier !== numOfKeys){
                    shapeMovement.rotation.x.multiplier += 1
                }else{
                    shapeMovement.rotation.x.multiplier = 1
                }
            }
        
        }
    
    }

}

I'd just like to increase the property value by one with whatever object and property I've thrown into that function.

I've seen another post where you can pass parameters, but this looks to assemble a new object, and is not by reference. I need to actually edit the values on the object's properties.

Originally, this is what I was trying, and it did not seem to alter the object on a global level. Only locally to the function:

incMultiplier(shapesMovements[index].rotation.x.multiplier)

function incMultiplier(multiplier){

    var numOfKeys = Object.keys(multipliers).length;
    if(multiplier !== numOfKeys){
        multiplier = multiplier + 1 
    }else{
        multiplier = 1
    }

    // always results in the same number. 
    // Does not keep increasing every time the function is called.
    console.log(multiplier); 
}
13
  • 1
    If im wrong correct me, Objects are inherently passed by reference. Commented Jun 22, 2020 at 17:44
  • 1
    What do you mean by "and properties"? An object is its properties. Commented Jun 22, 2020 at 17:45
  • 1
    What do multipliers and shapesMovements look like? Commented Jun 22, 2020 at 17:47
  • 2
    You can avoid two of those if statements by using shapeMovement[kind][dimension].multiplier. Or, if you never refer to other properties of shapeMovement, why not pass the shapesMovements[index].rotation.x object directly, and only manipulate its .multiplier property? Commented Jun 22, 2020 at 17:48
  • 1
    "It only alters it in the scope of the function." can you show a minimal reproducible example? Commented Jun 22, 2020 at 17:49

1 Answer 1

1

Originally, this is what I was trying

You're not passing an object with its properties there. You're passing the value of a single property, and assignments to multiplier do indeed just overwrite the local variable in the function. You need to pass an object and explicitly assign to its property:

function incMultiplier(valueObj) {
    var numOfKeys = Object.keys(multipliers).length;
    if (valueObj.multiplier !== numOfKeys) {
        valueObj.multiplier++;
    } else {
        valueObj.multiplier = 1
    }
}

incMultiplier(shapesMovements[index].rotation.x)
incMultiplier(shapesMovements[index].position.x)
incMultiplier(shapesMovements[index].rotation.y)
incMultiplier(shapesMovements[index].rotation.z)

It's not necessary to pass the whole shapesMovements objects and everything nested within them, passing a single mutable object is enough.

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

Comments

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.