Google’s John Mueller posted a very detailed response on Reddit about how Google treats canonicals and how JavaScript based sites that use them might run into issues. JavaScript isn’t the issue itself but it can cause some confusion.
Here is what John said:
As you can see, it is a complex topic but something that should work if done correctly.
Forum discussion at Reddit.