Skip to content

Instantly share code, notes, and snippets.

@tvandervossen
Created April 13, 2012 09:06
Show Gist options
  • Save tvandervossen/2375269 to your computer and use it in GitHub Desktop.
Save tvandervossen/2375269 to your computer and use it in GitHub Desktop.
How I Learned to Stop Worrying and Drop the Semicolon
device = function(){
var flags = {}, ua = navigator.userAgent, el = document.createElement('div')
function flag(names) {
document.body.className += (document.body.className ? ' ' : '') + names
names = names.split(' ')
for (var i = 0; i < names.length; i++) flags[names[i]] = true
}
if (ua.indexOf('MSIE ') > -1) flag('msie');
if (ua.indexOf('Firefox') > -1) flag('firefox')
if (ua.indexOf('(iPad') > -1) flag('ios ipad')
if (ua.indexOf('(iPhone') > -1 || ua.indexOf('(iPod') > 1) flag('ios iphone');
el.setAttribute('ongesturestart', 'return;')
if (typeof el.ongesturestart === 'function') flag('touch')
if (window.navigator.standalone) flag('standalone')
if (window.devicePixelRatio >= 2) flag('retina')
if(!!document.createElement('video').canPlayType) flag('video')
return flags
}()
device = (function() {
var flags = {}, ua = navigator.userAgent, el = document.createElement('div');
function flag(names) {
document.body.className += (document.body.className ? ' ' : '') + names;
names = names.split(' ');
for (var i = 0; i < names.length; i++) {
flags[names[i]] = true;
}
}
if (ua.indexOf('MSIE ') > -1) {
flag('msie');
}
if (ua.indexOf('Firefox') > -1) {
flag('firefox');
}
if (ua.indexOf('(iPad') > -1) {
flag('ios ipad');
}
if (ua.indexOf('(iPhone') > -1 || ua.indexOf('(iPod') > 1) {
flag('ios iphone');
}
el.setAttribute('ongesturestart', 'return;');
if (typeof el.ongesturestart === 'function') {
flag('touch');
}
if (window.navigator.standalone) {
flag('standalone');
}
if (window.devicePixelRatio >= 2) {
flag('retina');
}
if(!!document.createElement('video').canPlayType) {
flag('video');
}
return flags;
}());
@tvandervossen
Copy link
Author

Writing this piece of code made me finally decide to drop the semicolon and start following the Zepto.js code style (see https://github.com/madrobby/zepto#readme); it’s just so much more readable.

@teddyzetterlund
Copy link

I wouldn't say the (highly subjective) readability gain is so much caused by dropping the semicolon's as it is by embracing the loosely typed language characteristics in JavaScript (and getting rid of brackets for one line statements).

And on another note: Line 8 and 11 still has semicolon's :)

@patik
Copy link

patik commented Apr 13, 2012

Line 11, ua.indexOf('(iPod') > 1, you meant > -1, right?

Also, if your intent in using the opening parenthesis there is to prevent catching the string iPod as part of another word, you could also opt for the \b word boundary character and instead do /\biPod\b/.test(ua) in case the UA ever switches away from parens (and I believe regex is also faster).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment