DEV Community

Discussion on: TIL - Spread and Copying Objects in Javascript

 
pentacular profile image
pentacular

"if you are not sending snapshot of entire object, it is not pass-by-value" is obviously wrong.

Now, since it's not obviously wrong to you, let's go over some of the reasons. :)

Pass by value is about the value

So, what is the value?

Let's consider some operations that operate on values.

  1. Can we agree that a = b; assigns the value of b to a?
  2. Can we agree that a === b; compares the value of a and b?
  3. Can we agree that foo(a) passes the value of a to foo?
  4. Can we agree that foo(a) cannot change the value of a?

Please note any of these that you disagree with, and I'll point you at the section of the specification to explain why you're mistaken.

'snapshot of the entire object'

This is fundamentally incoherent -- what are the boundaries of an object?

Is it the reachable sub-graph of values given that object?

(In which case your argument will be that C++ does not support pass-by-value, since it doesn't necessarily copy these or consider them as part of the value)

Is it the immediate properties of the object?

(In which case your argument will be that C++ does not support pass-by-value, since it doesn't necessarily copy these or consider them as part of the value)

Is it something else? Feel free to propose some definition for 'entire snapshot'.

What pass-by-value and pass-by-reference actually mean

So, let's talk about how values work in C++.

C++ supports pass-by-value by requiring the programmer to establish a coherent protocol of copy and comparison operations.

This means that a = b; changes a such that a == b is true. And that given a == b; foo(a); cannot cause a != b to become true without involving side-effects.

This is what pass-by-value means.

C++ also supports pass-by-reference. In these cases foo(a) can cause a != b to become true, because the argument a and the parameter a in foo are effectively the same object.

So, which of these reflects how Javascript works?

If you say it reflects how C++ does pass-by-reference, it's trivial to disprove given the examples I've shown earlier.

Which is presumably why you keep on talking about 'pass-a-copy-of-a-reference'. I hope that it's clear that whatever you're calling it, it isn't pass-by-reference.

If not, I'll be happy to point out why you're mistaken -- just make a claim about the language specification or program behavior.

Good luck.

Thread Thread
 
canmingir profile image
Can Mingir • Edited

Gods of Coffee Mugs say you are so complicating such a simple concept :)

Thread Thread
 
pentacular profile image
pentacular

Let me know if you manage to produce a cogent response to any of the issues I've raised.

Thread Thread
 
canmingir profile image
Can Mingir

Now, since it's not obviously wrong to you

It is obvious to me and at least 791 people on first page.

stackoverflow.com/questions/131044...

Thread Thread
 
pentacular profile image
pentacular

Truth is not a popularity contest.

This is not a cogent response.

Let me know if you come up with one. :)

Thread Thread
 
canmingir profile image
Can Mingir

Let me know if you come up with one

Honestly, I don't know where to begin for someone fails to understand Wikipedia-level concept

Truth is not a popularity contest.

..and it is high school argument