Internet Explorer での JavaScript 配列関数の修正 (indexOf、forEach など) [closed] 質問する

Internet Explorer での JavaScript 配列関数の修正 (indexOf、forEach など) [closed] 質問する

詳細は他の場所、およびそれ以外は明らかによく知られている Internet Explorer (間違いなくバージョン 7、場合によってはバージョン 8) では、特にArray( forEach、、indexOfなど) の重要な機能が実装されていません。

回避策はいろいろありますが、コピー&ペーストしたり、独自の実装をハッキングしたりするのではなく、適切な標準的な実装セットをサイトに組み込みたいと思っています。js メソッドは有望に思えますが、別のライブラリの方がより推奨されているかどうかを確認するためにここに投稿しようと思いました。いくつかのさまざまな基準:

  • ライブラリは、ブラウザが既に実装している関数に対しては何も実行しないはずです (js-methodsここではかなりうまく機能しているようです)。
  • ライセンス、でも、お願いしますLGPL許容されます。

ベストアンサー1

多くはMDCフォールバック実装を使用しています(例:の指標)。これらは、すべての引数の型を明示的にチェックする程度まで、厳密に標準に準拠しています。

残念ながら、作者がこのコードを些細で自由に使用できるものとみなしていることは明らかですが、これを文書で明示するライセンス付与はないようです。 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;
    };
}

ここで実装されていないその他の ECMA262-5 メソッドには、Array reduce/ reduceRight、JSON メソッド、およびObjectJS 関数として確実に実装できるいくつかの新しいメソッドが含まれます。

おすすめ記事