Google's John Mueller said in a video hangout that Google can technically process canonicals within JavaScript. This is assuming that what is shown in the JavaScript doesn't conflict with other signals. John confirmed this on Twitter when Patrick Stox summarized it.
John said this at the 12:35 mark into the video when the question was presented:
Question from Darcy Burk: I guess my question is implementing hreflang and then modifying a canonical tag using JavaScript. So you see that being an issue or something that Google would recognize?
John Mueller's Answer: That could work.
Here is the video embed because John then goes into some of the difficulty around this:
Here is Patrick's tweet summarizing it with John from Google confirming:
Yep
— John ☆.o(≧▽≦)o.☆ (@JohnMu) August 29, 2018
Forum discussion at Twitter.