Call by sharing is explicitly mentioned at the bottom of this article and has, as far as my formal education goes, no consensus in definition, which is why I am hesitant in using that nomenclature, but yes. It is call by sharing given the definition you linked. That doesn't really change anything here.
Isn't someObject not changed, because in the function the copy reference myRefMaybe is pointing to a totally new object { key: 42 },
I don't quite understand this sentence. I think the point I was trying to make is that, ifmyRefMaybe was a reference, then re-assigning the value of that reference, would change the original definition too. But in JavaScript, it's a copy of a reference. Thus, re-assigning it doesn't change the original, but modifying the referenced object does change the original (as both the original and the argument / copy of the reference point to the same memory that stores the object).
A reference is simply a memory address that is automatically dereferenced by the language/runtime.
From that point of view
// (01) primitiveValue stores address #AvarprimitiveValue=1;// (02) someObject stores address #BvarsomeObject={is:'changed?'};// (03) myRefMaybe stores address #A from primitiveValuereference_assignment(primitiveValue);// (06) primitiveValue still has address #AprimitiveValue;// (07) myRefMaybe stores address #B from someObjectreference_assignment(someObject);// (10) someObject still has address #BsomeObject;functionreference_assignment(myRefMaybe){// (04) myRefMaybe still has address #A// (08) myRefMaybe still has address #BmyRefMaybe={key:42};// (05) myRefMaybe now has address #C// (09) myRefMaybe now has address #D}
if myRefMaybe was a reference, then re-assigning the value of that reference, would change the original definition too
To be able to change the "original definition" you would need the reference to the someObject reference itself (rather than the reference to the original object):
if someObject stores the address #B to the actual object
and that address #B is stored on behalf of someObject at address #Z
you need to place address #C at address #Z so that someObject now stores the address #C to that other object.
Update:
Call by Value: Copy the actual value.
Call by Sharing: Copy the address to the value.This makes it possible to share the value but not possible to replace the original value at the source variable.
Call by Reference: Copy the address of the "variable". This makes it possible to replace the original value at the source variable.
The way JavaScript behaves it actually never has to store anything at the address that is found in a variable - it only stores new addresses in variables.
// (1) `myRefMaybe` has address #B as set by callermyRefMaybe={key:42};// (2) New value `{ key: 42 }` is created// (3) `myRefMaybe` stores address #C to new value
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Call by sharing is explicitly mentioned at the bottom of this article and has, as far as my formal education goes, no consensus in definition, which is why I am hesitant in using that nomenclature, but yes. It is call by sharing given the definition you linked. That doesn't really change anything here.
I don't quite understand this sentence. I think the point I was trying to make is that, if
myRefMaybe
was a reference, then re-assigning the value of that reference, would change the original definition too. But in JavaScript, it's a copy of a reference. Thus, re-assigning it doesn't change the original, but modifying the referenced object does change the original (as both the original and the argument / copy of the reference point to the same memory that stores the object).When you put it like this is more clear to me. Thanks
A reference is simply a memory address that is automatically dereferenced by the language/runtime.
From that point of view
To be able to change the "original definition" you would need the reference to the
someObject
reference itself (rather than the reference to the original object):someObject
stores the address #B to the actual objectsomeObject
at address #ZsomeObject
now stores the address #C to that other object.Update:
The way JavaScript behaves it actually never has to store anything at the address that is found in a variable - it only stores new addresses in variables.