0

I've been looking at some Javascript/AngularJS code and something that stood out to me was this:

var searchModel = $scope.searchModel = new SearchModel('id');

I haven't seen that before and I haven't been able to find a good explanation online for it. I was wondering if someone could tell me exactly what's going on with this code?

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Pr0pagate
  • 199
  • 2
  • 12
  • It's just a shorthand way of assigning both `searchModel` and `$scope.searchModel` to the same value – user184994 Nov 07 '18 at 17:21
  • It's creating a new `SearchModel` and assigning it to `$scope.searchModel` and a local variable named `searchModel`. ["Chaining the assignment operator is possible in order to assign a single value to multiple variables."](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Assignment_2) – JLRishe Nov 07 '18 at 17:21
  • Gotcha. So in this case, is it really just for convenience, i.e. so you don't have to type "$scope.searchModel" and can just type "searchModel"? – Pr0pagate Nov 07 '18 at 17:32

1 Answers1

1

Javascript objects are mutable , that means they are references , so when you use equal to operator , scan for left you will assign the new SearchModel('id'); to scope variable and now that points to that objects means they are nothing but the same , now this scope variable is assigned to var variable which will again contain the same object reference.