DEV Community

Discussion on: Troll Hunting 101: JavaScript Passes Objects by Reference

Collapse
 
gadreash profile image
gadreash • Edited

@bytebodger , excellent article! I have coded in c++ before and am now on the Java/ JavaScript spectrum. Instead of debating pass-by-value vs by-ref semantics, people should focus on understanding what is the end result.
Agreed the aforementioned terms might mean the same but the situation/usecases in which they become relevant is where your expertise/mastery of the language comes into picture.
I usually visualize this problem using food (food always makes things easier ;)).
Function argument vars are like bowls. Apple in that bowl is like an object passed to the function. If the function takes that apple and inserts something inside it then the function caller will see that change in the apple.
On the other hand if the function replaces that apple with an orange then the apple remains unchanged in the scope where the function was called from.

If you are able to understand the above then you are set for life. If people really want to debate what should something be called then they can focus on Potato/Tomato pronunciations!!
PS: I used function for example, the above analogy works even for regular var to var assignment/copy :)