在Internet Explorer中修复JavaScript数组函数(indexOf,forEach等) [英] Fixing JavaScript Array functions in Internet Explorer (indexOf, forEach, etc.)

查看:129
本文介绍了在Internet Explorer中修复JavaScript数组函数(indexOf,forEach等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如详细其他地方和其他显然众所周知的Internet Explorer(绝对版本7,在某些情况下,版本8 )不实现键函数,特别是在 Array (例如 forEach indexOf ,等)。



这里有很多解决方法,但我想把一个正确的,我们的网站,而不是复制和粘贴或hack在我们自己的实现。我找到了 js-methods ,看起来很有前途,但我认为发布在这里,看看是否另一个图书馆更加强烈推荐。其他一些标准:




  • 库应该只是浏览器已经实现的函数的一个无操作c $ c> js-methods 似乎在这里做得很好)。

  • 非 - GPL ,请 LGPL

    许多使用MDC回退的实现(例如 =https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf =nofollow noreferrer> indexOf )。他们通常严格遵守标准,即使是在显式检查所有参数的类型。



    不幸的是,虽然很清楚,作者认为这个代码微不足道和可自由使用,似乎没有明确的许可证授予以书面形式。维基作为一个整体是CC Attribution-ShareAlike,如果这是一个可接受的许可证(虽然CC不是为代码设计的)。



    js-一般,但是不是作为标准围绕如何函数的边缘(例如未定义的列表项,使列表变异的函数)。它还充满了其他随机的非标准方法,包括一些有问题的,像狡猾的stripTags和不完全的UTF-8编解码器(这也是有点不必要给定 unescape(encodeURIComponent) trick)。



    为什么值得,这里是我使用的(我现在释放到公共领域,如果它可以说是所有的版权) 。它比MDC版本有点短,因为它没有尝试输入嗅探你没有做过一些愚蠢的事情,比如传递非函数回调或非整数索引,但除了它试图符合标准。 (让我知道,如果我错过了任何东西; - ))

     'use strict' 

    //如果不支持本机则添加ECMA262-5方法绑定
    //
    if(!(Function.prototype中的'bind')){
    函数。 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)));
    };
    }
    };
    }

    //添加ECMA262-5字符串修饰(如果不支持本机)
    //
    if(!('trim'in String.prototype)){
    String.prototype.trim = function(){
    return this.replace(/ ^ \s + /,'').replace(/ \s + $ /,'');
    };
    }

    //添加ECMA262-5数组方法(如果不支持本机)
    //
    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 if(i in this& this [i] === find)
    return i;
    return -1;
    };
    }
    if(!(Array.prototype中的'lastIndexOf')){
    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 ++因为从参数可悲地包含* /
    if(i in this& this [
    return i;
    return -1;
    };
    }
    if(!('forEach'in Array.prototype)){
    Array.prototype.forEach = function(action,that / * opt * /){
    (var i = 0,n = this.length; i if(i in this)
    action.call(that,this [i],i,this)
    };
    }
    if(!(Array.prototype中的'map')){
    Array.prototype.map = function(mapper,that / * opt * /){
    var other = new Array(this.length);
    for(var i = 0,n = this.length; i if(i in this)
    other [i] = mapper.call i],i,this);
    return other;
    };
    }
    if(!(Array.prototype中的'filter')){
    Array.prototype.filter = function(filter,that / * opt * /){
    var other = [],v;
    for(var i = 0,n = this.length; i if(i in this& filter.call(that,v = this [i],i ,this))
    other.push(v);
    return other;
    };
    }
    if(!(Array.prototype中的'every')){
    Array.prototype.every = function(tester,that / * opt * /){
    (var i = 0,n = this.length; i if(i in this&!tester.call(that,this [i],i,this))
    return false;
    return true;
    };
    }
    if(!(Array.prototype中的'some')){
    Array.prototype.some = function(tester,that / * opt * /){
    (var i = 0,n = this.length; i if(i in this& tester.call(that,this [i],i,this))
    return true;
    return false;
    };
    }

    此处未实现的其他ECMA262-5方法包括Array 减少 / reduceRight ,JSON和几个新的 Object 作为JS函数实现。


    As detailed elsewhere, and otherwise apparently well-known, Internet Explorer (definitely version 7, and in some instances, version 8) do not implement key functions, in particular on Array (such as forEach, indexOf, etc).

    There are a number of workarounds here and there, but I'd like to fold a proper, canonical set of implementations into our site rather than copy and paste or hack away at our own implementations. I've found js-methods, which looks promising, but thought I'd post here to see whether another library comes more highly-recommended. A couple of miscellaneous criteria:

    • The library should just be a no-operation for those functions that a browser already has implementations for (js-methods appears to do quite well here).
    • Non-GPL, please, though LGPL is acceptable.

    解决方案

    Many use the MDC fallback implementations (eg. for indexOf). They're generally rigorously standards-compliant, even to the extent of explicitly checking the types of all the arguments.

    Unfortunately whilst it is clear that the authors regard this code as trivial and freely-usable, there doesn't seem to be an explicit licence-grant to put this in writing. The wiki as a whole is CC Attribution-ShareAlike, if that's an acceptable licence (though CC isn't designed for code as such).

    js-methods looks OK in general, but is not as standards-compliant around the edges of how the functions are supposed to be (eg. undefined list items, functions that mutate the list). It's also full of other random non-standard methods, including some questionable ones like the dodgy stripTags and the incomplete UTF-8 codec (which is also a bit unnecessary given the unescape(encodeURIComponent) trick).

    For what it's worth, here's what I use (which I hereby release into the public domain, if it can be said to be copyrightable at all). It's a bit shorter than the MDC versions as it doesn't attempt to type-sniff that you haven't done something silly like pass non-function callbacks or non-integer indexes, but apart from that it attempts to be standards-compliant. (Let me know if I've missed anything. ;-))

    '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;
        };
    }
    

    Other ECMA262-5 methods not implemented here include Array reduce/reduceRight, the JSON ones and the few new Object methods that can be reliably implemented as JS functions.

    这篇关于在Internet Explorer中修复JavaScript数组函数(indexOf,forEach等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆