Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将箭头函数转换为函数表达式_Javascript_Internet Explorer - Fatal编程技术网

Javascript 将箭头函数转换为函数表达式

Javascript 将箭头函数转换为函数表达式,javascript,internet-explorer,Javascript,Internet Explorer,我有一个带有贴图对象的函数: 函数xml\u编码 { 返回数组.from.map(c=> { var cp=c.codepoint(0); 返回((cp>127)“&#”+cp+'”:c); }).加入(“”); } 这非常有效,只是在运行InternetExplorer11时,它破坏了一切 我试图使用函数表达式重写代码,但我得到的c未定义: 函数xml\u编码 { 返回数组.from.map(函数() { var cp=c.codepoint(0); 返回((cp>127)“&#”+cp+'

我有一个带有贴图对象的函数:

函数xml\u编码
{
返回数组.from.map(c=>
{
var cp=c.codepoint(0);
返回((cp>127)“&#”+cp+'”:c);
}).加入(“”);
}
这非常有效,只是在运行InternetExplorer11时,它破坏了一切

我试图使用函数表达式重写代码,但我得到的
c
未定义:

函数xml\u编码
{
返回数组.from.map(函数()
{
var cp=c.codepoint(0);
返回((cp>127)“&#”+cp+'”:c);
}).加入(“”);
}

不幸的是,这需要一个面向公众的功能,我现在需要支持IE11。如何重写此函数以与IE11一起使用?

您缺少函数的参数,请尝试此操作

函数xml\u编码{
返回数组.from.map(函数c){
var cp=c.codepoint(0);
返回((cp>127)“&#”+cp+'”:c);
}).加入(“”);
}

您的匿名函数缺少
c
参数

function xml_encode(s)
{
  return Array.from(s).map(
    function(c) {
      //use charCodeAt for IE or use a polyfill
      //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt#Polyfill
      var cp = c.codePointAt(0); 
      return ((cp > 127) ? '&#' + cp + ';' : c);
    }
  ).join('');
}
奖金:
您可能希望使用
s.split(“”)
而不是
Array.from(s)
,以获得更好的性能和浏览器支持<每个浏览器都支持code>String.prototype.split,而IE中不支持
Array.from
<代码>拆分在我的电脑上,在Chrome上快30%,在FF上快80%。

尝试按以下方式修改代码(函数中缺少参数):

因为您使用的是Array.from和codepoint函数,所以它们不支持IE浏览器。要在IE浏览器中使用它们,我们需要在使用此函数之前添加相关的popyfill

function xml_encode(s)
{
  return Array.from(s).map(
    function(c) {
      //use charCodeAt for IE or use a polyfill
      //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt#Polyfill
      var cp = c.codePointAt(0); 
      return ((cp > 127) ? '&#' + cp + ';' : c);
    }
  ).join('');
}
Polyfill代码如下(我已经创建了一个示例来测试它,它在我这边运行良好):

//ECMA-262第6版22.1.2.1的生产步骤
如果(!Array.from){
Array.from=(函数(){
var toStr=Object.prototype.toString;
var isCallable=函数(fn){
返回类型fn==“函数”| | toStr.call(fn)===“对象函数]”;
};
var toInteger=函数(值){
var编号=编号(值);
if(isNaN(number)){返回0;}
如果(number==0 | |!isFinite(number)){returnnumber;}
返回(编号>0?1:-1)*数学楼层(数学abs(编号));
};
var maxsafeinger=Math.pow(2,53)-1;
var toLength=函数(值){
var len=toInteger(值);
返回Math.min(Math.max(len,0),maxSafeInteger);
};
//from方法的长度属性为1。
从(arrayLike/*,mapFn,thisArg*/)返回函数{
//1.设C为该值的最大值。
var C=这个;
//2.将项目设为对象(类似于阵列)。
变量项=对象(类似于阵列);
//3.返回故障(项目)。
if(arrayLike==null){
抛出新的TypeError('Array.from需要类似于数组的对象-不为null或未定义');
}
//4.如果mapfn未定义,则让mapping为false。
var mapFn=arguments.length>1?参数[1]:未定义为void;
变量T;
如果(映射类型Fn!==“未定义”){
//5.其他
//5.a如果IsCallable(mapfn)为false,则抛出TypeError异常。
如果(!isCallable(mapFn)){
抛出新的TypeError('Array.from:提供时,第二个参数必须是函数');
}
//5.b.如果提供了thisArg,则T为thisArg;否则T为未定义。
如果(arguments.length>2){
T=参数[2];
}
}
//10.获取lenValue(项目“长度”)。
//11.让len为ToLength(lenValue)。
var len=总长度(项目长度);
//13.如果IsConstructor(C)为真,则
//13.a.让a作为调用[[Construct]]内部方法的结果
//使用包含单个项len的参数列表的C。
//14.a.否则,让a成为ArrayCreate(len)。
var A=isCallable(C)?对象(新的C(len)):新的数组(len);
//16.设k为0。
var k=0;
//17.当k    // Production steps of ECMA-262, Edition 6, 22.1.2.1
    if (!Array.from) {
        Array.from = (function () {
            var toStr = Object.prototype.toString;
            var isCallable = function (fn) {
                return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
            };
            var toInteger = function (value) {
                var number = Number(value);
                if (isNaN(number)) { return 0; }
                if (number === 0 || !isFinite(number)) { return number; }
                return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
            };
            var maxSafeInteger = Math.pow(2, 53) - 1;
            var toLength = function (value) {
                var len = toInteger(value);
                return Math.min(Math.max(len, 0), maxSafeInteger);
            };

            // The length property of the from method is 1.
            return function from(arrayLike/*, mapFn, thisArg */) {
                // 1. Let C be the this value.
                var C = this;

                // 2. Let items be ToObject(arrayLike).
                var items = Object(arrayLike);

                // 3. ReturnIfAbrupt(items).
                if (arrayLike == null) {
                    throw new TypeError('Array.from requires an array-like object - not null or undefined');
                }

                // 4. If mapfn is undefined, then let mapping be false.
                var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
                var T;
                if (typeof mapFn !== 'undefined') {
                    // 5. else
                    // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
                    if (!isCallable(mapFn)) {
                        throw new TypeError('Array.from: when provided, the second argument must be a function');
                    }

                    // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
                    if (arguments.length > 2) {
                        T = arguments[2];
                    }
                }

                // 10. Let lenValue be Get(items, "length").
                // 11. Let len be ToLength(lenValue).
                var len = toLength(items.length);

                // 13. If IsConstructor(C) is true, then
                // 13. a. Let A be the result of calling the [[Construct]] internal method 
                // of C with an argument list containing the single item len.
                // 14. a. Else, Let A be ArrayCreate(len).
                var A = isCallable(C) ? Object(new C(len)) : new Array(len);

                // 16. Let k be 0.
                var k = 0;
                // 17. Repeat, while k < len… (also steps a - h)
                var kValue;
                while (k < len) {
                    kValue = items[k];
                    if (mapFn) {
                        A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
                    } else {
                        A[k] = kValue;
                    }
                    k += 1;
                }
                // 18. Let putStatus be Put(A, "length", len, true).
                A.length = len;
                // 20. Return A.
                return A;
            };
        }());
    }

    /*! https://mths.be/codepointat v0.2.0 by @mathias */
    if (!String.prototype.codePointAt) {
        (function () {
            'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
            var defineProperty = (function () {
                // IE 8 only supports `Object.defineProperty` on DOM elements
                try {
                    var object = {};
                    var $defineProperty = Object.defineProperty;
                    var result = $defineProperty(object, object, object) && $defineProperty;
                } catch (error) { }
                return result;
            }());
            var codePointAt = function (position) {
                if (this == null) {
                    throw TypeError();
                }
                var string = String(this);
                var size = string.length;
                // `ToInteger`
                var index = position ? Number(position) : 0;
                if (index != index) { // better `isNaN`
                    index = 0;
                }
                // Account for out-of-bounds indices:
                if (index < 0 || index >= size) {
                    return undefined;
                }
                // Get the first code unit
                var first = string.charCodeAt(index);
                var second;
                if ( // check if it’s the start of a surrogate pair
                    first >= 0xD800 && first <= 0xDBFF && // high surrogate
                    size > index + 1 // there is a next code unit
                ) {
                    second = string.charCodeAt(index + 1);
                    if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
                        // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
                        return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
                    }
                }
                return first;
            };
            if (defineProperty) {
                defineProperty(String.prototype, 'codePointAt', {
                    'value': codePointAt,
                    'configurable': true,
                    'writable': true
                });
            } else {
                String.prototype.codePointAt = codePointAt;
            }
        }());
    }