Javascript 为什么angular.lowercase方法使用';按位或32';转换';A';至';a';?为什么不使用'+;32';?
上面是角度代码。为什么使用“ch.charCodeAt(0)| 32”将“A”转换为“A”?为什么不使用'ch.charCodeAt(0)+32'?因为32恰好是2的幂,Javascript 为什么angular.lowercase方法使用';按位或32';转换';A';至';a';?为什么不使用'+;32';?,javascript,Javascript,上面是角度代码。为什么使用“ch.charCodeAt(0)| 32”将“A”转换为“A”?为什么不使用'ch.charCodeAt(0)+32'?因为32恰好是2的幂,c | 32相当于c+32,如果c&32==0(即c在32的位置有一个0)。按位运算通常比加法稍快,因为计算机可以同时计算所有位,而不必将进位连成链 var manualLowercase = function(s) { return isString(s) ? s.replace(/[A
c | 32
相当于c+32
,如果c&32==0
(即c
在32的位置有一个0)。按位运算通常比加法稍快,因为计算机可以同时计算所有位,而不必将进位连成链
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;
}