914avg
Last Updated: February 25, 2016
·
626
· srph

On javascript deep-linking (note)

This is usually something nobody cares about, not until you're dealing with something important. I'm not sure what "important" is, but when you're dealing with maybe, something like:

var a = { name: 'Pogi', age: 69 };

You want 'a' to be the same as 'b'

var b = a;

Then you want 'b' to have a different age, say. 23?
b.age = 23;

What do you expect?
// Both produces 23 console.log(a); console.log(b);

The same goes for arrays
```
var a = [1, 2, 3];
var b = a;

b[0] = 5;
console.log(a[0]);
a[0]produces5```.

But why? Because, theoretically, when you assign an object or array to a variable, you are linking the two by reference.

To be honest, I've never made a research about this; I apologize for whatever misconception or mistakes I may include here. The example is something impractical, but do deliberate!

If you need to deep-link both (assigning the value of a to b without linking the two by reference), there are available algorithms and concepts around the internet database. AngularJS' angular.copy is something to consider.