why the third option is better than regex?

I think regex is pretty fast and the third option is confusing. What do you think?

http://jqfundamentals.com/book/ch09s12.html

// old way
if (type == 'foo' || type == 'bar') { ... }

// better
if (/^(foo|bar)$/.test(type)) { ... }

// object literal lookup 
if (({ foo : 1, bar : 1 })[type]) { ... }

5
задан Nick Vanderbilt 16 October 2010 в 02:19
поделиться