5 ms·
> even the difference between passing by reference vs value You can only pass by value in JS so maybe that's why. It is a bit confusing because of how objects
by searchableguy 6y ago
> even the difference between passing by reference vs value
You can only pass by value in JS so maybe that's why. It is a bit confusing because of how objects work that it feels like you are passing by reference. You are passing reference as the value.
- brundolf 6y agoWhen it comes to primitives you can only pass by value, but with objects and arrays you can only pass by reference. And this applies to comparisons too: when comparing objects or arrays you are comparing by reference, not deeply by their inner values. If you want to compare them deeply you have to take some additional steps to make that possible, and all of the different strategies for doing so have different trade-offs. I once talked to someone with >5 years front-end experience, including some team-lead experience, who thought the difference between == and === was that === compares objects deeply. That is very, dangerously wrong. The only way I can imagine he got by for so long with that misconception is that he'd always used ImmutableJS.
- Scarbutt 6y agobut with objects and arrays you can only pass by reference. It's still pass by value, you are passing the reference of the object as a value.
- brundolf 6y agoI'm not sure I agree with your framing of the precise terminology ("passing a reference to an object, by value" vs "passing an object by reference"), but it also isn't important. What's important is this: let a = { foo: "bar" } let b = { foo: "bar" } console.log(a === b) // false and this: let a = { foo: "bar" } let b = a b.foo = "blah" console.log(a.foo) // "blah" This is the kind of misunderstanding that causes insidious bugs.
- slibhb 6y ago> I'm not sure I agree with your framing of the precise terminology ("passing a reference to an object by value" vs "passing an object by reference") The distinction is that, if JS truly was pass-by-reference for objects, you could change what the reference was pointing to. But you can't. I agree with you that objects/references are a stumbling block for new programmers in JS. You have to understand how these things work but there's no explicit concept of a reference, which means it can be tricky to learn.
- brundolf 6y ago> you could change what the reference was pointing to That's only true if you're passing the pointer by reference. That's different from passing the object itself by reference. I did just look up the C++ semantics and learned something: technically reference-arguments don't have to be implemented by the compiler as pointers, though in practice they mostly are. So in that sense it's technically more correct to say that JS works with object pointers and not references. But we're really getting quite deep into the weeds at that point. https://stackoverflow.com/questions/5893873/pass-by-reference-vs-pass-by-pointer#5893950 https://stackoverflow.com/questions/5893873/pass-by-referenc...
- ithrow 6y agoThis is the kind of misunderstanding that causes insidious bugs. That's how all mainstream languages with a GC work, Java, C#, Ruby and Python, it's not particular to JS. And this why why languages like Clojure are nice to work with. but it also isn't important It's important if you really want to know why your examples work they way they do.
- searchableguy 6y agoI have been very curious about clojure. What do you feel is the most attractive part about it?
- brundolf 6y ago> it's not particular to JS We're specifically talking about new programmers who haven't used any of those languages, and who are being reared in the JavaScript ecosystem where certain patterns reign that obscure this language behavior. That's what the original discussion was about. > It's important if you really want to know why your examples work they way they do. It's really not. Pass-by-reference and pass-a-pointer-by-value are subtly different concepts even in the C++ world, and using terminology that involves the word "pointer" opens up a whole other can of stuff that needs explaining.
- searchableguy 6y ago> When it comes to primitives you can only pass by value, but with objects and arrays you can only pass by reference. A variable stores reference to the original object and when you pass it to a function, it passes the actual reference as the value. I think the last part you mentioned is actually confusing in js because lack of clear guidance. isXyz, Xyz.isXyz, Object.is, and other ways are used in practice to do the same thing but they all have different behavior which trips people.
- culturedsystems 6y agoEverything in JavaScript is passed the same way, whether primitive values or objects. Trying to graft a distinction between pass-by-value and pass-by-reference on top of this single passing method is unlikely to do anything other than confuse people learning JavaScript.
- brundolf 6y agoYou're talking about the call site; I'm talking about the semantics. There is undeniably a crucial semantic distinction, whatever terminology you want to use to talk about it.
- asiachick 6y agoYou don't pass by reference in JS. You pass references by value. Passing by reference has a well defined meaning in computer language terminology. It means passing something that references the variable outside the function. Something you can't do in JavaScript void someFunc(ref int v) { v = 456; } var foo = 123; someFunc(foo); write(foo); // output 456 Above we are not passing 123 (the value of foo) into someFumc, we are passing a reference to foo. This is what pass by reference means. same if foo was reference to an object void someFunc(ref Object v) { v = someOtherObject; } var foo = someObject; someFunc(foo); // foo now refers to someOtherObject In this impossible in JavaScript. It does not have the concept of pass by reference. It only has pass by value function someFunc(v) { v = someOtherObject; } var foo = someObject; someFunc(foo); // foo still refers to someObject Above the value of foo, which is a reference to someObject, that value is assigned to v in someFunc. v's value is set to reference someOtherObject. foo's value is still someObject as there is no way to "pass by reference" in JavaScript
- porker 6y agoGreat explanation of something that has caught me out multiple times (I occasionally dabble in JS). Having spent my career in languages with pass by reference it feels unnatural not to have it. I suspect if I started with JS this wouldn't be such a problem.
- u02sgb 6y agoI would argue that Javascript is a hybrid of pass by reference because of the case below though. function someFunc(v) { v.myvar = 456; v = someOtherObject; } var foo = someObject; foo.myvar = 123; someFunc(foo); // foo still refers to someObject // foo.myvar is 456 Banning our team from passing objects around except in specific, discussed, cases got rid of lots of difficult to track down bugs.
- asiachick 6y agoYou'd be wrong. Again "Pass by reference" is "well defined" in computer languages. Passing a reference to an object is not "pass by reference". Your example works the same in Java and C# and probably Swift and many other languages. But you aren't "passing by reference". "pass by reference" specifically means passing a reference to the variable, not the variable's value. Javascript always passes the value of the variable, it has no ability to pass a variable by reference.