如何為InternetExplorer瀏覽器修復(fù)JavaScript中的Array indexOf如果您使用過任何長度的JavaScript,那么您就知道InternetExplorer沒有為Array.Prototype.indexOf()[包括InternetExplorer 8]實(shí)現(xiàn)ECMAScript函數(shù)。這不是一個(gè)大問題,因?yàn)槟梢允褂靡韵麓a擴(kuò)展頁面上的功能。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;}我應(yīng)該在什么時(shí)候?qū)崿F(xiàn)這一點(diǎn)?我是否應(yīng)該用下面的檢查將它包裝在我的所有頁面上,檢查Prototype函數(shù)是否存在,如果不存在,那么繼續(xù)擴(kuò)展Array Prototype嗎?if (!Array.prototype.indexOf) {
// Implement function here}或者進(jìn)行瀏覽器檢查,如果它是InternetExplorer,那么只需實(shí)現(xiàn)它?//Pseudo-codeif (browser == IE Style Browser) {
// Implement function here}
如何為InternetExplorer瀏覽器修復(fù)JavaScript中的Array indexOf
尚方寶劍之說
2019-06-19 16:01:58