this post was submitted on 15 Oct 2024
10 points (100.0% liked)
JavaScript
2000 readers
1 users here now
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Having to pass in null values seems a bit weird. You can define functions and optional parameters like this:
Then people don't have to call your function with
they just call your library with
You could add a default inside the method signature, like:
because if you define it in the method:
then if people still call it with
Then the default
c = 5
is overwritten by null, and results in 0.I don't know if you really need to handle all that though, instead of just doing
c = 5
- if people intentionally call your library with null, and things go wrong...? well yea ok, don't do that then.But it depends on the use-case. If this is some method deep within a library, and some other calling method might be unintentionally dumping null into it, you could default it inside the method, and handle it