var manualLowercase = function(s) {
return isString(s)
? s.replace(/[A-Z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) | 32);})
: s;
};
var manualUppercase = function(s) {
return isString(s)
? s.replace(/[a-z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) & ~32);})
: s;
};
// String#toLowerCase and String#toUpperCase don't produce correct results in browsers with Turkish
// locale, for this reason we need to detect this case and redefine lowercase/uppercase methods
// with correct but slower alternatives. See https://github.com/angular/angular.js/issues/11387
if ('i' !== 'I'.toLowerCase()) {
lowercase = manualLowercase;
uppercase = manualUppercase;
}上面是angular代码。为什么使用'ch.charCodeAt(0) | 32‘将'A’转换为'a'?为什么不是'ch.charCodeAt(0) + 32‘呢?
发布于 2017-08-04 09:40:40
因为32恰好是2的幂,所以如果c & 32 === 0 (即c在32的位置上有0),则c | 32等同于c + 32。逐位运算通常比加法略快,因为计算机可以同时计算所有位,而不必链接进位。
https://stackoverflow.com/questions/45496982
复制相似问题