cem*_*ick 137 javascript internet-explorer cross-browser
至于详细的其他地方,否则显然是众所周知的,IE浏览器(版本绝对7,并在某些情况下,版本8)不落实的关键功能,特别是Array(如forEach,indexOf等).
这里和那里有许多变通方法,但我想将一组适当的,规范的实现折叠到我们的网站中,而不是复制,粘贴或破解我们自己的实现.我找到了js-methods,看起来很有前景,但我想在这里发帖看看是否有更高度推荐的另一个库.一些杂项标准:
bob*_*nce 220
许多人使用MDC回退实现(例如,对于indexOf).它们通常严格遵守标准,甚至在明确检查所有参数类型的范围内.
不幸的是,虽然很明显作者认为这些代码是微不足道的,并且可以自由使用,但似乎并没有明确的许可授予书.整个wiki是CC Attribution-ShareAlike,如果这是一个可接受的许可证(虽然CC不是为代码设计的).
js-methods一般看起来很好,但是并不像函数应该是如何符合标准(例如,未定义的列表项,改变列表的函数).它也充满了其他随机的非标准方法,包括一些可疑的方法,如狡猾的stripTags和不完整的UTF-8编解码器(根据unescape(encodeURIComponent)技巧也有点不必要).
对于它的价值,这就是我使用的内容(我在此公布了域名,如果它可以说是可以受版权保护的话).它比MDC版本短一些,因为它不会尝试打字,你没有像传递非函数回调或非整数索引这样愚蠢的东西,但除此之外,它试图符合标准.(如果我错过了什么,请告诉我.;-))
'use strict';
// Add ECMA262-5 method binding if not supported natively
//
if (!('bind' in Function.prototype)) {
Function.prototype.bind= function(owner) {
var that= this;
if (arguments.length<=1) {
return function() {
return that.apply(owner, arguments);
};
} else {
var args= Array.prototype.slice.call(arguments, 1);
return function() {
return that.apply(owner, arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments)));
};
}
};
}
// Add ECMA262-5 string trim if not supported natively
//
if (!('trim' in String.prototype)) {
String.prototype.trim= function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
}
// Add ECMA262-5 Array methods if not supported natively
//
if (!('indexOf' in Array.prototype)) {
Array.prototype.indexOf= function(find, i /*opt*/) {
if (i===undefined) i= 0;
if (i<0) i+= this.length;
if (i<0) i= 0;
for (var n= this.length; i<n; i++)
if (i in this && this[i]===find)
return i;
return -1;
};
}
if (!('lastIndexOf' in Array.prototype)) {
Array.prototype.lastIndexOf= function(find, i /*opt*/) {
if (i===undefined) i= this.length-1;
if (i<0) i+= this.length;
if (i>this.length-1) i= this.length-1;
for (i++; i-->0;) /* i++ because from-argument is sadly inclusive */
if (i in this && this[i]===find)
return i;
return -1;
};
}
if (!('forEach' in Array.prototype)) {
Array.prototype.forEach= function(action, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
action.call(that, this[i], i, this);
};
}
if (!('map' in Array.prototype)) {
Array.prototype.map= function(mapper, that /*opt*/) {
var other= new Array(this.length);
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
other[i]= mapper.call(that, this[i], i, this);
return other;
};
}
if (!('filter' in Array.prototype)) {
Array.prototype.filter= function(filter, that /*opt*/) {
var other= [], v;
for (var i=0, n= this.length; i<n; i++)
if (i in this && filter.call(that, v= this[i], i, this))
other.push(v);
return other;
};
}
if (!('every' in Array.prototype)) {
Array.prototype.every= function(tester, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this && !tester.call(that, this[i], i, this))
return false;
return true;
};
}
if (!('some' in Array.prototype)) {
Array.prototype.some= function(tester, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this && tester.call(that, this[i], i, this))
return true;
return false;
};
}
Run Code Online (Sandbox Code Playgroud)
此处未实现的其他ECMA262-5方法包括Array reduce/ reduceRight,JSON和Object可以作为JS函数可靠实现的少数新方法.
Kris Kowal编译了一个小型库,作为ECMAScript 5功能的垫片,可能在浏览器的实现中缺失.其他人已经多次修改了一些功能,以便针对速度进行优化并解决浏览器错误.编写的函数尽可能地遵循规范.
es5-shim.js是在MIT许可下发布的,Array.prototype扩展名靠近顶部,你可以很容易地删除和删除你不需要的任何函数.我还建议你缩小脚本,因为评论使它比它需要的大得多.
| 归档时间: |
|
| 查看次数: |
78045 次 |
| 最近记录: |