Skip to content
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

illegal invocation #3439

Closed
sacrosanctic opened this issue Jan 22, 2024 · 4 comments
Closed

illegal invocation #3439

sacrosanctic opened this issue Jan 22, 2024 · 4 comments

Comments

@sacrosanctic
Copy link
Contributor

Why doesn't it work? Isn't randomUUID a function?

// does work
times(()=>crypto.randomUUID(),5)

// doesnt work
times(crypto.randomUUID,5)

repro

@semmel
Copy link
Contributor

semmel commented Jan 22, 2024

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.

@sacrosanctic
Copy link
Contributor Author

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?

@semmel
Copy link
Contributor

semmel commented Jan 22, 2024

@sacrosanctic

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 thiscrypto to work. 🤔

@kedashoe
Copy link
Contributor

ty for the explanation @semmel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants