Use defaults for undefined arguments in shorthand call to swal
See original GitHub issueI found some unexpected behavior when working on #979…
main()
async function main () {
swal.setDefaults({type: 'question'})
await swal({ title: 'I am the question type...'})
await swal('but I am not?')
}
I would expect the second swal to also have type
of 'question'
, but it does not.
It’s due to this line of code, which assigns the values to params
even when they are undefined
…
I propose that we treat undefined
arguments as such, and use the parameter defaults in these cases. What do y’all think?
I’m not sure if this would be considered a fix or a breaking change. I mean, I’m pretty sure it’s technically a breaking change, but bug fixes usually are. How likely is it that people are relying on the current behavior?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Be careful when using || to set default values in JavaScript
When no value is passed into a function the value of the parameter will be undefined . Using OR however means that the...
Read more >Destructuring assignment - JavaScript - MDN Web Docs
Each destructured property can have a default value. The default value is used when the property is not present, or has value undefined...
Read more >30 Awesome JavaScript Shorthand Techniques That You ...
The shorthand techniques in any programming language help you write code in a much cleaner, optimized, and shorter form.
Read more >Is there a way to check for both `null` and `undefined`?
Using a juggling-check, you can test both null and undefined in one hit: if (x == null) {. If you use a strict-check,...
Read more >Functions - Elixir School
Using anonymous functions is such a common practice in Elixir there is ... As you probably already guessed, in the shorthand version our...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Good. Yeah I agree.
I was thinking of something like this:
I’ll put in another PR after #992 settles
Aha, it’s amusing to see that we always agree on complex subjects and general direction but always fail to find a compromise on the smallest details 😆
It’s not code duplication at this point 😃 You are a bit extreme here. It’s just a small, pure-JS pattern applied to three variables. Our brain likes those visual layouts and patterns. Those LoCs don’t need to be fully read, parsed and analyzed. At the contrary, the array, the function, the loop, the array access syntax, the if branch, the two levels of indentation: they make this code look complicated. I need to read those lines when scanning the code. It’s all about cognitive load (athough the performance is also reduced, but I agree it’s not that relevant here).
But don’t worry, I’m not asking you to change it if you don’t agree, I don’t care that much about that. I’m just saying that I would have not written it like that. And I’m sorry to have started another unproductive debate 😄