正如您所见,here(中文),此代码可能用于测试Chrome。 编辑:请参见下面的完整故事 ..
正如在文章中解释,什么情况是,Chrome的优化“的.sort(...)”方法,以这样的方式使[0, 0].sort(...)
调用不会执行给定的比较功能。
从文章中,Chrome的执行情况 “的.sort(...)” 是这样的:
function sort(comparefn) {
var custom_compare = (typeof(comparefn) === 'function');
function Compare(x,y) {
if (x === y) return 0;
if (custom_compare) {
return comparefn.call(null, x, y);
}
...
}
由于0 === 0
是真实的,它不会叫comparefn
。
在jQuery的情况下,它不会将全局变量baseHasDuplicate
设置为false
。
编辑:如果你浏览灒的源代码,here例如(进入“灒CSS选择器引擎”下的黄色区域,被称为“灒变量”),你会发现下面的解释:
var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,
done = 0,
toString = Object.prototype.toString,
hasDuplicate = false,
baseHasDuplicate = true;
// Here we check if the JavaScript engine is using some sort of
// optimization where it does not always call our comparision
// function. If that is the case, discard the hasDuplicate value.
// Thus far that includes Google Chrome.
[0, 0].sort(function(){
baseHasDuplicate = false;
return 0;
});
看起来很神秘!
这就是有点苛刻考虑有简单的方法来检查 – Dani 2010-08-10 03:10:18
哦,有编程无处不在。有时候人们真的很喜欢做事的坏方法:) – 2010-08-10 03:11:18
@Dani:确实!但是,代码可能用于其他方面......从“Javascript忍者”中听到这将非常棒。 – 2010-08-10 03:14:14