Skip to content

Instantly share code, notes, and snippets.

@lovio
Last active August 29, 2015 14:00
Show Gist options
  • Save lovio/11107223 to your computer and use it in GitHub Desktop.
Save lovio/11107223 to your computer and use it in GitHub Desktop.
a usage of `~`
res.type = function(type){
return this.set('Content-Type', ~type.indexOf('/')
? type
: mime.lookup(type));
};
# 判断Cotent-Type中是否有/,比如application/json
# 如果有就去查询mime
# 在js中, null, undefined , '' 以及0都是false
# ~-1 === 0
# ~按位取反
# 这样就避免了使用 ~type.indexOf('/') === -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment