-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add() unexpectedly mutates the original object. #955
Comments
I got bit by this as well, until I read Moment Clone.
Thus now I assume most methods will mutate the original object. I think both approaches are valid, but mutation at least matches the original You have to call m = moment() # let's say it's Oct 3rd.
oct6 = m.clone().add('days', 3)
oct7 = moment(m).add('days', 4) |
This is not a bug, its a feature! Read a discussion here: #390 It don't think this is ever going to change, at least because of compatibility issues. |
This feature sucks. |
@akenverus As moment is no longer being actively developed, it is advisable to switch your projects to another library. |
eg:
oct6 is now oct 6th, as expected, but m has also changed, unexpectedly
if add() had operated in a functional manner without mutating state, oct7 would in fact be set to october 7th, but because add() mutated the state of m, it's set to october 10th!
in fact, oct6 and oct7 are now the same object, m, and both reference october 10th.
this seems really unintuitive and can easily cause unintended consequences when moment objects are passed around in non-trivial applications.
The text was updated successfully, but these errors were encountered: