illegal invocation
sacrosanctic opened this issue · comments
Why doesn't it work? Isn't randomUUID a function?
// does work
times(()=>crypto.randomUUID(),5)
// doesnt work
times(crypto.randomUUID,5)
Isn't randomUUID a function?
No, in the browser it is not a free static function, but needs to be bound to crypto
:
// works in the browser — does not work in Nodejs
times(crypto.randomUUID.bind(crypto), 5);
Funnily enough, in Nodejs crypto.randomUUID
is a free static function, but your example won't work there either. This is because — if called with an argument — it expects that optional argument to be an object.
You'll get another error message
TypeError: The "options" argument must be of type object. Received type number (0)
It's still an awful mess trying to write isomorphic JavaScript. (Mostly the messy browser apis imo)
Btw. same issues with console.log
😵💫
Supplement:
On the other hand JSON.parse
again is static in the browser.
Who decides that stuff anyway, and why? It's massively annoying for FP: there seems to be no scheme.
Thanks for the detailed answer, I was not aware of a free static function
. What kind of function is crypto.randomUUID
on the browser if it is not a free static function?
What kind of function is
crypto.randomUUID
on the browser if it is not a free static function?
A "method for the crypto
namespace" perhaps?
All I know is that in C++ one calls functions which are provided by objects and operate on their (private) data "methods". In JS that would apply to functions which use this
in their implementation. So perhaps randomUUID
somehow needs this
≡ crypto
to work. 🤔
ty for the explanation @semmel