I’m reading someone’s source code for their backbone app and trying to figure out this toggle function. I understand the basic of toggle is that it just switches back and forth between a setting. In the example below, it sets the favorite on a model (indicating if it’s a favorite or not, I assume). I also understand the use of ! (meaning not). However, I don’t get the logic of how this function works.
toggle: function() {
this.save({favorite: !this.get("favorite")});
},
In particular, I don’t get the
!this.get("favorite")
Isn’t this.get("favorite") returning a string, namely “favorite”? or is this.get(“favorite”) returning the attribute which is a boolean?
Can someone kindly explain in plain english how the logic of this function works each time it would be called, toggling between the two states.
! is a negation.
The function probably returns a boolean, which result then is inverted.
So in this case it would toggle
favoritebetween true and false