-1

I have this function :

static calculatePrice(offer)
{
    if (typeof (offer.TakerGets || offer.taker_gets) == "string")
        offer.quality = 1 / offer.quality;

    return offer.quality / 1000000;
}

I don't understand why the function modifies the value that I pass to it (this.book[currency][side][this.j[currency][side]]). It does not modify a copy but the original as if I had passed it a pointer.

roannav
  • 105
  • 1
  • 9
  • That's how JavaScript works. Objects are passed as references, they're not copied. – Barmar Apr 28 '23 at 17:41
  • 1
    JS never makes copies of objects or arrays by itself, you have to write it explicitly if you want a copy. – Barmar Apr 28 '23 at 17:42
  • This is actually quite the interesting question, JavaScript is actually not pass-by-reference apparently, but it would affect the original object in your case. You can find a very nice detailed answer in this duplicate: [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/q/518000/13927534) – Miss Skooter Apr 28 '23 at 17:45
  • You should use `===` rather than `==` and also check `offer.quality` is non-zero before division. – jarmod Apr 28 '23 at 17:53
  • Thank you for all your responses, so fast! Actually, since the object is a value of a property, I didn't understand why it was doing this, but it makes sense to me now. – user18227572 Apr 28 '23 at 17:59

1 Answers1

2

When you pass an object into a function (or assign it from one variable to another), you're passing a value that's called an object reference, which tells JavaScript where the object is in memory. (Yes, it's a bit like a pointer in some other languages.) You're not passing in a copy of the object. So yes, the function can change the state of that object by modifying its properties, etc.

Let's take a simpler example:

function example(arg) {
    arg.answer = 42;     // Modifies the state of the object
}

let obj = {};
example(obj);            // Passes a value to the function saying where the
                         // object is in memory
console.log(obj.answer); // 42

In that code, the example function can modify the properties of the object, because what was passed in is an object reference.

It's important to note that JavaScript is still purely pass-by-value, it's just that the value being passed is an object reference. What the example function can't do is change the obj variable that was used in example(obj) by assigning a different object to arg (arg = someOtherObject.) Doing that only affects arg, not obj. (There's no link from arg back to obj, they both just have the same value in them.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hmmm. Maybe this *is* a duplicate of [*Is JavaScript a pass-by-reference or pass-by-value language?*](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) I thought a more targeted answer was called for, but others may decide to close the question, which is fair enough. – T.J. Crowder Apr 28 '23 at 17:51
  • Oh yes, your last point is important to know. – user18227572 Apr 28 '23 at 18:44