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?

3
  • It's just a shorthand way of assigning both searchModel and $scope.searchModel to the same value Commented Nov 7, 2018 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." Commented Nov 7, 2018 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"? Commented Nov 7, 2018 at 17:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.