【发布时间】:2013-01-24 04:33:15
【问题描述】:
最近我在 jQuery 源代码中发现了一个奇怪的行(最新版本 1.9.1,Sizzle 包,第 129 行 funescape 函数):
funescape = function( _, escaped ) {
var high = "0x" + escaped - 0x10000;
// NaN means non-codepoint
return high !== high ? // <--- LINE 129
escaped :
// BMP codepoint
high < 0 ?
String.fromCharCode( high + 0x10000 ) :
// Supplemental Plane codepoint (surrogate pair)
String.fromCharCode( high >> 10 | 0xD800, high & 0x3FF | 0xDC00 );
};
进行high !== high比较的原因是什么?看起来return escaped 永远不会被执行。还是我错过了什么?
【问题讨论】:
-
可能是一个错误?那个函数的预期签名是什么?我还看到一个未使用的
_参数,... -
@redShadow 好吧,
_是可以理解的,因为出于某种原因(可能是为了保持兼容性)作者只需要获取第二个参数,除了我会使用arguments[1]来代替。跨度> -
我认为正如评论所说,
NaN !== NaN将总是返回true -
@VisioN: 使用
arguments会有相当高的性能成本。在像嘶嘶声这样每个毫秒都可以计算的库中,不使用arguments可能是更明智的选择stackoverflow.com/questions/5325554/…
标签: javascript jquery