代码之家  ›  专栏  ›  技术社区  ›  RA19

ES6与ES5浏览器兼容Array.Prototype.Includes和IndexOf

  •  1
  • RA19  · 技术社区  · 6 年前

    我有下面的java脚本,在Chrome、Firefox中运行得很好,但在IE中不起作用。我相信问题出在array.prototype.includes和array.prototype.indexof上,但是不知道如何解决这个问题。

    return memo.indexOf(索引)>-1?getRandom(arr,memo):索引;

    var mapping = {
      R:
        "1111111111111111111111000000111100000011110000001111111111101111111000110000110011000001101100000011",
      T:
        "1111111111111111111100001100000000110000000011000000001100000000110000000011000000001100000000110000"
    
    };
    
    // Grab the binary mapping of the letter and
    function binaryise(letter) {
      var arr = mapping[letter].split("");
      return arr
        .map(function(char) {
          return (
            '<div class="' +
            (char === "0" ? "zero" : "one") +
            '">' +
            char +
            "</div>"
          );
        })
        .join("");
    }
    
    // For each letter in the word create a
    // binary version and return it in a list-item container
    function processWord(arr) {
      var items = arr
        .map(function(letter, i) {
          var binaryised = binaryise(letter);
          return (
            '\n      <li class="binaryli" data-id=' +
            i +
            '>\n        <div class="containerbinary">' +
            binaryised +
            "</div>\n      </li>\n    "
          );
        })
        .join("");
      return '<ul class="binaryul">' + items + "</ul>";
    }
    
    // Get a random number that hasn't previously appeared
    
    function getRandom(arr, memo) {
      var index = Math.floor(Math.random() * arr.length);
      return memo.indexOf(index) > -1 ? getRandom(arr, memo) : index;
    }
    
    // Once the html has been added to the page
    // (all set with opacity to 0)
    // iterate over the letters turning the
    // opacity of each to 1
    function showLetters(arr, memo) {
      memo = memo || [];
      if (memo.length !== arr.length) {
        var index = getRandom(arr, memo);
        var letter = arr[index];
        var el = document.querySelector('[data-id="' + index + '"]');
        setTimeout(function() {
          el.classList.add("show");
          memo.push(index);
          showLetters(arr, memo);
        }, 1000);
      }
    }
    
    var wordArr = "RT".toUpperCase().split("");
    
    // Process all the letters of the word and add them
    // to the page...
    var html = processWord(wordArr);
    output.insertAdjacentHTML("beforeend", html);
    
    // ...then fade them in
    showLetters(wordArr);
            <section id="binary">
                <div id="contactbinary" class="containersbinary">
                    <div class="row">
                        <div class="text-center">
                            <div id="output">
    
                            </div>
                            </div>
                        </div>
    
                        </div>
    </section>

    Below output is what happens in IE

    Below output of second image is what appears in chrome (correctly)

    1 回复  |  直到 6 年前
        1
  •  1
  •   Treycos    6 年前

    根据 this link includes 任何版本的IE都不支持。

    然而 you can still use indexOf

    你可以找到你的ES5ed密码 here