rserota / wad

Web Audio DAW. Use the Web Audio API for dynamic sound synthesis. It's like jQuery for your ears.
MIT License
1.9k stars 160 forks source link

(Fix) Add utility function to avoid type coercion errors when using || to assign default values #25

Closed jgladch closed 9 years ago

jgladch commented 9 years ago

Hello,

First of all, thanks for all the work you've put into this library. I'm using it a lot in a project I've been working on and I've run into a few bugs that I decided to clean up.

There were many parts of the library that attempt to assign default values using the following pattern:

this.volume = arg.volume || 1;

Due to the idiosyncrasies of javascript, when arg.volume is '0', it is interpreted as undefined and the default value of 1 is used instead.

In a lot of places, the way you were assigning defaults didn't matter. Cases like:

arg.env.attack || 0

Because the default value was 0, it didn't matter.

To fix this issue, I've implemented a utility function that handles the defaulting for us. I've done my best to keep your one-liner style intact as much as possible.

Thanks!

rserota commented 9 years ago

Thanks for contributing.

If you wanted to show me your project (or any projects you've done with Wad.js), I'd be curious to see it.

jgladch commented 9 years ago

As soon as we deploy to production I'll be sure to send you a link. Thanks for the merge. May have more bugfixes coming up soon.