41 ms·
Mutability, in this case, can be pretty unexpected. Just like you don't expect the expression a + 5 to change the value of a but to return a new number, most p
by bbotond 6y ago
Mutability, in this case, can be pretty unexpected.
Just like you don't expect the expression a + 5 to change the value of a but to return a new number, most people don't expect myDate.add(5, 'days') to change the value of myDate.
I know that moment values are mutable, yet I often forget it while writing code and make dangerous mistakes because of how unnatural it is to see an arithmetic expression that is not immutable.
- mywittyname 6y agoThat's totally expected behavior to me. I would expect to use something like Moment.addDate(date1, 5, 'days') if I wanted a new instance of a date to be returned. Even in javascript-land, methods like that will have side-effects, yet still return this often just for the sake of method chaining. E.g. date.add(5, 'days).format("%Y-%m-%d") would mutate date, but return the same instance as a convenience.