如果您已经使用过任何长度的JavaScript,那么您就知道Internet Explorer没有为Array.prototype.indexOf()[包括Internet Explorer 8]实现ECMAScript函数.这不是一个大问题,因为您可以使用以下代码扩展页面上的功能.
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) { return i; }
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
我什么时候应该实现这个?
我应该使用以下检查将其包装在我的所有页面上,检查是否存在原型函数,如果不存在,请继续并扩展Array原型?
if (!Array.prototype.indexOf) {
// Implement function here
}
Run Code Online (Sandbox Code Playgroud)
或者浏览器检查,如果它是Internet Explorer,那么只需实现它?
//Pseudo-code
if (browser == IE Style Browser) {
// Implement function here
}
Run Code Online (Sandbox Code Playgroud) javascript internet-explorer cross-browser internet-explorer-8
我!!~在阅读时在代码中发现了一个奇怪的内容:https://github.com/LearnBoost/mongoose/blob/master/lib/document.js#L678
Document.prototype.isModified = function (path) {
return !!~this.modifiedPaths.indexOf(path);
};
Run Code Online (Sandbox Code Playgroud)
我读过那是什么!! (不是)JavaScript中的运算符?以及如何:〜运算符?; 为什么作者!!~在这里使用?
我试过了:
!!~1 // -> true
!!~0 // -> true
!!~-1 // -> false
!!~-2 // -> true
Run Code Online (Sandbox Code Playgroud)
似乎它只是false在数字的时候-1.这样对吗?为什么不核对一下电话号码是不是-1还是>=0?
javascript operators bitwise-operators mongoose comparison-operators