3 ms·
"Create-copy" and "create-alias" are better ways to describe the concepts. This post describes pass-by-value as create-copy, and pass-by-reference as create-ali
by stephengillie 9y ago
"Create-copy" and "create-alias" are better ways to describe the concepts. This post describes pass-by-value as create-copy, and pass-by-reference as create-alias.
Create-copy - Copies the data into a new memory location, so modifying this variable will not modify the original variable.
Create-alias - Creates a new name for the same memory location, so modifying one variable will also modify the other variable.
Having spent too much time in Powershell, which defaults to create-copy, it was jarring to switch to Javascript and the seemingly-non-deterministic behavior of create-alias on setting a new variable equal to an array. From an algebraic perspective, when I modify "foo", I expect "bar" to remain the same.
- Zarel 9y agoThe confusion comes up when you do a _shallow_ copy: Some modifications will modify the original variable, and other modifications won't. Pass-reference-by-value is just a special case of a shallow copy. let a = [1, 2, 3]; let b = a; b[1] = 5; // will modify a b = [1, 6, 3]; // will not modify a In languages like C++, there's a way to make it so that last line _would_ modify a, which is why there's a need to use different words for the different phenomenon.