Skip to content

Instantly share code, notes, and snippets.

@jhkueh
Created May 29, 2020 03:52
Show Gist options
  • Save jhkueh/41496c482e38ac7c84f0b9f3b68dabde to your computer and use it in GitHub Desktop.
Save jhkueh/41496c482e38ac7c84f0b9f3b68dabde to your computer and use it in GitHub Desktop.
function destructuring assigned parameters were not passed by reference
var a = { a: 1, b: 2, c:3 };
var b = { d: 4, e:5 };
function d1(obj) {
obj.a = 5;
}
d1(a)
// a = { a: 5, b: 2, c: 3 }
function d2(obj) {
obj.c = b;
}
d2(a)
// a = { a: 5, b: 2, c: { d: 4, e:5 } }
// function destructuring assigned parameters were not passed by reference
function d3({ a, b, c }) {
c = 7;
}
d3(a)
// a = { a: 5, b: 2, c: { d: 4, e:5 } }
// a.c was not updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment