News for hackers. Stuff that matters.
Re:Deep-copying in JavaScript by andrewstuart2 on Thursday January 25, 2018 @05:03PM (#16235120)
"By reference" means different things to different people.

The most accurate statement I've found is that JavaScript, much like Java, passes reference-by-value. Everything is passed by value, but if your value is an object, then the new scope gets a copy of the reference. If you modify the object by dereferencing (that is, `a.foo = "bar";`), your modifications to the object persist outside of your scope.

This is an important understanding to have when working with JavaScript.

Re:Deep-copying in JavaScript by pjmlp on Thursday January 25, 2018 @05:18PM (#16235241)
Everything is passed by value.

A numeric value is put into the call stack.

Which in the case of references happens to be the address of pointee.

It is only pass by reference, if what is put into the call stack is the address of the actual variable on the caller stack holding the data.

Re:Deep-copying in JavaScript by andrewstuart2 on Thursday January 25, 2018 @05:22PM (#16235267)
Isn't that what I said? That's what I was trying to say, at least, but keeping the language more js-oriented.
Re:Deep-copying in JavaScript by ZeroBugBounce on Thursday January 25, 2018 @06:31PM (#16235761)
I'm not the parent commenter but yes, you both made the same distinction as far as I can tell.
Re:Deep-copying in JavaScript by Stratoscope on Thursday January 25, 2018 @07:23PM (#16236059)
It doesn't help us understand the language if we try to shoehorn JavaScript's behavior into one of two buckets: "pass by value" or "pass by reference."

As you mentioned elsewhere, these terms are well defined in computer science. But how JavaScript works doesn't match either of them.

It only creates confusion to force JavaScript assignment and parameter passing into one of those two categories. This is what has led to the widespread misconception that "primitives are passed by value, and objects are passed by reference."

We're not implementing JavaScript engines here, we're programming in JavaScript. Implementation details like addresses and pointers are unknown to our code. They may differ from one JavaScript engine to another, and even during a single run of your program as the engine decides on the fly how to optimize or de-optimize your code.

What matters is the observable behavior from the point of view of the running JavaScript code.

There are a few ways you could describe that behavior, such as my notion of a new "name" that refers to an existing "thing", or shkkmo's and andrewstuart2's "pass a reference by value". But it's not "pass by value" as that term is understood in other programming languages such as C/C++.

Re:Deep-copying in JavaScript by pjmlp on Friday January 26, 2018 @03:23AM (#16237722)
No, JavaScript only does pass by value, nothing else.

There is nothing to discuss here other than JavaScript developers trying to imagine stuff instead of learning the underlying literature concepts.

JavaScript is not a special snowflake language.

Computer Science describes how things are supposed to be, they don't change because of language X or Y.

Re:Deep-copying in JavaScript by BeeOnRope on Thursday January 25, 2018 @08:50PM (#16236444)
It's essentially exactly analogous to pass-by-value in C, where references are replaced by pointers. The primary difference is that JavaScript (and many other langues) use references as the only way to refer to an object, whereas C also has the concept of directly referring to an object by value where assignment or passing as a parameter will make a copy of the object.
Re:Deep-copying in JavaScript by k2xl on Thursday January 25, 2018 @05:33PM (#16235344)
It isn't "if the value is an object then scope gets a copy of the reference" No new objects are created when you pass an object.

An object's value is a memory location. When you pass it as an argument you're passing that value into the method. The analogy is more akin to casting than copying.