DEV Community

Discussion on: TIL - Spread and Copying Objects in Javascript

 
pentacular profile image
pentacular

See this bit here?

A Reference is a resolved name or property binding.

Not an object.

An example of using a reference in Javascript is

delete a.b;

where a.b is a reference to a property to be deleted.

I suggest re-reading the section more carefully -- but you are at least on the right track to figuring this out properly. :)

Thread Thread
 
canmingir profile image
Can Mingir

You got so confused "base value" of the the runtime with "primitive value" in ECMA, but my real problem is you are not able to fundamentally distinguish between pass-by-value and pass-by-reference with pure Computer Science terminologies, then I can explain why do they copy references.

Again, it is quite straightforward:

We use the same concept with message brokers in event driven design, which follows same principles.

Thread Thread
 
pentacular profile image
pentacular

Read the algorithm in the ecmascript specification.

Show the part which has a(b) copy a reference. :)

In the process you may come to understand what's happening here and perhaps you'll even come to understand what pass-by-reference means.

Good luck.

Thread Thread
 
canmingir profile image
Can Mingir

12.3.6.2 talking about steps of reference check with different types, and it is super clear.

But I want to back off a bit, can we get at least some consensus on "if you are not sending snapshot of entire object, it is not pass-by-value" theorem, I want to make sure you know what that is, then I promise I'll get you all details one by one.

Thread Thread
 
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