Passing a pointer/reference to a variable as parameter

后端 未结 5 1752
情话喂你
情话喂你 2021-01-24 17:40

I know this question has been asked multiple times (yes, I did some research) but I can\'t see to find a solution that fits my needs.

What I have done so far:

5条回答
  •  孤城傲影
    2021-01-24 18:16

    In JavaScript values such as integers, strings, etc. are passed by value. If you want to pass a reference, you have to pass an object into the JavaScript function. (JavaScript objects are passed by reference)

    function adjustValues(referenceObject) {
        referenceObject.foo = 2;
        referenceObject.bar = "newValue";
    }
    
    referenceObject = {
      foo: 1,
      bar: "initialValue"
    };
    
    adjustValues(referenceObject);
    

提交回复
热议问题