0

I'm a self-learning coder and I'm not good enough so far. Question is:

We know that for non-primitives (i.e. Object), references are in the stack while values are in the heap. How about the nested object? IMO because of the different results of the shallow and deep copy of an multidimensional object, the nested object is just a reference in its parent object stored in the heap. Where are the values of the nested object stored at, another space in the heap? Also in javascript we cannot visit data in the heap directly, how do we manipulate the nested object? stack ref → heap ref → heap val chaining???

I'm a green bird and really confused.

P.S. I've seen at least 2 different definitions of shallow/deep copy:
1. Shallow one copies the reference only while deep one copies reference & value
2. Shallow one makes one-dimensional copy while deep one copies every single dimension
Which is correct?

trincot
  • 317,000
  • 35
  • 244
  • 286
legenddaniel
  • 698
  • 2
  • 9
  • 17
  • FWIW: An implementation must adhere to the semantics, not the other way around. ECMAScript does not even define a 'reference' in that manner. Regardless, since ECMAScript uses **assign/call by [object] sharing** then all objects 'exist on the heap' (also not defined in, or relevant to, the specification) FSVO in implementations, *discounting potential implementation optimizations/details*. – user2864740 Jan 19 '20 at 03:32
  • its like another languages about stack and memory usage just cant access to variable pointer directly , for detail please read this article, it may help : https://blog.sessionstack.com/how-javascript-works-memory-management-how-to-handle-4-common-memory-leaks-3f28b94cfbec – Neo Anderson Jan 19 '20 at 03:34
  • That is, a shallow copy is a shallow copy and a deep copy is a deep copy. It either works as advertised or does not, *independent* of any implementation which may or may not use pointers / heaps / etc. The ECMAScript specification can be found online, as can many questions about shallow and deep copies in JavaScript.. – user2864740 Jan 19 '20 at 03:38
  • Does this answer your question? [JavaScript by reference vs. by value](https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value) , https://stackoverflow.com/q/518000/2864740 (there are many more) – user2864740 Jan 19 '20 at 03:41
  • @user2864740 But these two are totally irrelevant defs – legenddaniel Jan 19 '20 at 03:41
  • Not sure what "defs" refers to. In any case, the two links above cover all relevant semantics. (Not all of the answers are great, but there is a sufficient number that are, and details abound.) I prefer the simple explanation: 1) **an assignment [including passing an argument] does *not* copy/clone/duplicate an object** and; 2) **an object is itself**. – user2864740 Jan 19 '20 at 03:42

0 Answers0