简体   繁体   English

不支持ES6扩展语法IE

[英]ES6 spread syntax IE not supported

I have a javascript code that is given below that is ES6 compatible however IE 11 does not support this. 我在下面给出了与ES6兼容的javascript代码,但是IE 11不支持此代码。 What would be the replacement code for this such that it works across all browsers? 这样在所有浏览器中都可以使用的替换代码是什么?

[...document.querySelectorAll('.row')]

Im using this for 'click' event handling: 我正在使用它来进行'click'事件处理:

Array.prototype.slice.call(document.querySelectorAll('.row'))
    .forEach(function(header) {
      return header.addEventListener('click', function(e) {
        headerClick(e, header, header.querySelector('.exy'))
      });
    });

For all browsers, you can use Array.prototype.slice via call or apply (it works on any array-like object): 对于所有浏览器,您可以通过callapply使用Array.prototype.slice (它适用于任何类似数组的对象):

Array.prototype.slice.call(document.querySelectorAll('.row'))

About your updated question: 关于您更新的问题:

Im using this for 'click' event handling: 我正在使用它来进行'click'事件处理:

 Array.prototype.slice.call(document.querySelectorAll('.row')) .forEach(function(header) { return header.addEventListener('click', function(e) { headerClick(e, header, header.querySelector('.exy')) }); }); 

I wouldn't use querySelectorAll for this at all, I'd use event delegation . 我根本不会使用querySelectorAll ,我会使用事件委托 Presumably all of those .row elements are inside a common container (ultimately, of course, they're all in body , but hopefully there's a container "closer" to them than that). 大概所有这些.row元素都在一个公共容器内(当然,最终它们全都在body ,但是希望有一个比它们更“接近”的容器)。 With event delegation, you do this: 使用事件委托,您可以执行以下操作:

  • Hook click just once, on the container 钩一次,在容器上click

  • When a click occurs, check to see if it passed through one of your target elements en route to the container 发生点击时,检查它是否在到达容器的途中通过了您的目标元素之一

For your quoted code, that looks something like this: 对于您引用的代码,看起来像这样:

// A regex we'll reuse
var rexIsRow = /\brow\b/;
// Hook click on the container
document.querySelector("selector-for-the-container").addEventListener(
    "click",
    function(e) {
        // See if we find a .row element in the path from target to container
        var elm;
        for (elm = e.target; elm !== this; elm = elm.parentNode) {
            if (rexIsRow.test(elm.className)) {
                // Yes we did, call `headerClick`
                headerClick(e, elm, elm.querySelector('.exy'));
                // And stop looking
                break;
            }
        }
    },
    false
);

On more modern browsers , you could use elm.classList.contains("row") instead of the regular expression, but sadly not on IE9 or earlier. 在更现代的浏览器上 ,可以使用elm.classList.contains("row")代替正则表达式,但遗憾的是,不能在IE9或更早的版本上使用。


That said, rather than maintaining a separate codebase, as gcampbell pointed out you could use ES6 (ES2015) features in your code and then transpile with a transpiler that converts them (well, the ones that can be converted, which is a lot of them) to ES5 syntax. 就是说,而不是像gcampbell指出的那样维护一个单独的代码库,您可以在代码中使用ES6(ES2015)功能,然后使用可将其转换的编译器进行编译(嗯,可以转换的功能很多) )转换为ES5语法。 Babel is one such transpiler. 巴别塔就是其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM