DEV Community

Discussion on: Weird behaviors of javascript: Primitive Types and Reference Types

 
pentacular profile image
pentacular

but in the case of this article, it's a bit different: when doing a = b, it's important to understand that both variables share (or reference) the same underlying value (composite, mutable) in memory

So what this mental model does is to say that the values of a and b aren't the real values of a and b, they're special reference values, and the real, underlying value, is something else which depends on the properties.

Which leads to people making errors like saying that foo(a) is pass by reference because the value you're passing is a 'reference value' and the 'real value' of a is determined by the set of its properties.

But there's nothing in javascript which represents this 'real value', and no operators that operate on this 'real value' (because it doesn't actually exist in the language).

So you end up demoting the actual accessible value and promoting an imaginary and inaccessible kind of value and confusing the language with things like pass-by-copy-of-reference (which seems to be the fallback position when it's pointed out that it isn't actually pass-by-reference).

But at least I'm starting to get an idea of where these peculiar ideas are coming from -- so thanks for being able to talk through it. :)