我正在查看Express中respons.js的代码,发现了以下代码:
我的问题是,
res.contentType =
res.type = function(type){
return this.set('Content-Type', ~type.indexOf('/')
? type
: mime.lookup(type));
};
我的问题是,
~
运算符在type.indexOf()
语句前面是做什么用的?它的目的是什么,何时使用?
~type.indexOf('/')
的意思是 "type
包含字符串/
"。 - Myrne StolString.prototype.contains = function(s) { return ~this.indexOf(s); }
,这将使type.contains('/')
成立。 - EricindexOf
返回0
时,表示已找到匹配(在字符串的开头),因此您希望使用真值来指示匹配的存在。~0
等于-1
,这是一个真值。 - apsillers