Pass by value (sort of) in AngularJS by copy()ing
Recently a colleague asked about passing by value rather than JavaScript's funky pass-by-something (i.e. for an object, by value but that value is itself a reference).
Passing by value for an object is more-or-less really making a copy.
Angular provides angular.copy(source[, destination])
that will create a deep copy of the source - find the documentation here
In addition, if you want to compare two things to see if they are truly equal (i.e. a deep equals that will ignore value/reference stuff) then use angular.equals()
- and that documentation is here
As always, a deep copy/equals is just that: deep. So try not to compare huge objects.
At the end of the day, my colleague found a better way of achieving her end goal - knowing about angular.copy()
then would've probably resulted in a worse solution.
Written by Oliver Tupman
Related protips
1 Response
And what was a solution for your colleague's problem?