Javascript:在IE上遇到.map()问题

时间:2012-07-13 22:03:25

标签: javascript jquery internet-explorer map

此代码在Chrome,FFX等上运行良好。它获取textarea的内容并分隔不同数组项中的所有行(新行由空数组项表示)。在IE上测试它时会抛出错误。代码:

这是tregex的价值和电话:

var tregex = /\n|([^\r\n.!?]+([.!?]+|$))/gim;

var source = $('#text').val().match(tregex).map($.trim);

由于 .map()(仅限IE),代码会抛出此错误消息

  

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 5.1;   三叉戟/ 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR   3.5.30729)时间戳:星期五,2012年7月13日21:52:48 UTC

     

消息:对象不支持此属性或方法Line:128   字符:6代码:0 URI:http://mydomain.com/src/common.js

为什么呢?我能用IE7 +支持吗? (这是在IE8上测试的)。

4 个答案:

答案 0 :(得分:2)

IE是一个糟糕的浏览器,因此没有内置的地图功能(至少在IE7-8中没有)。由于您尝试在正则表达式匹配的结果上调用map(而不是在jQuery结果对象上调用它),因此您可以使用的唯一映射是内置映射(IE没有)。

有许多库可以为您模拟地图,包括jQuery,Underscore和Mochikit。

这是一个如何使用jQuery来做你想做的事情的例子:

$.map($('#text').val().match(tregex), $.trim);

答案 1 :(得分:1)

我相信您的代码可能会偶然在FF和Chrome中运行。您的意思是使用jQuery.map吗?您正在使用{9}之前的IE中不支持的Array.map

请考虑以下内容作为替代。

var source = $.map($('#text').val().match(tregex), $.trim);

这使用jQuery的map实现来循环并进行修剪。

答案 2 :(得分:0)

我相当确定Array.map()仅在IE9中添加。你应该手动循环遍历数组。

答案 3 :(得分:0)

您只需添加shim即可添加支持:

// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.com/#x15.4.4.19
if (!Array.prototype.map) {
  Array.prototype.map = function(callback, thisArg) {

    var T, A, k;

    if (this == null) {
      throw new TypeError(" this is null or not defined");
    }

    // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
    var O = Object(this);

    // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = O.length >>> 0;

    // 4. If IsCallable(callback) is false, throw a TypeError exception.
    // See: http://es5.github.com/#x9.11
    if ({}.toString.call(callback) != "[object Function]") {
      throw new TypeError(callback + " is not a function");
    }

    // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
    if (thisArg) {
      T = thisArg;
    }

    // 6. Let A be a new array created as if by the expression new Array(len) where Array is
    // the standard built-in constructor with that name and len is the value of len.
    A = new Array(len);

    // 7. Let k be 0
    k = 0;

    // 8. Repeat, while k < len
    while(k < len) {

      var kValue, mappedValue;

      // a. Let Pk be ToString(k).
      //   This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
      //   This step can be combined with c
      // c. If kPresent is true, then
      if (k in O) {

        // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
        kValue = O[ k ];

        // ii. Let mappedValue be the result of calling the Call internal method of callback
        // with T as the this value and argument list containing kValue, k, and O.
        mappedValue = callback.call(T, kValue, k, O);

        // iii. Call the DefineOwnProperty internal method of A with arguments
        // Pk, Property Descriptor {Value: mappedValue, Writable: true, Enumerable: true, Configurable: true},
        // and false.

        // In browsers that support Object.defineProperty, use the following:
        // Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });

        // For best browser support, use the following:
        A[ k ] = mappedValue;
      }
      // d. Increase k by 1.
      k++;
    }

    // 9. return A
    return A;
  };      
}

您还可以在IE7-8中添加es5shim添加.map以及所有其他缺少的数组方法以及其他商品,例如Function#bind