编程语言
首页 > 编程语言> > javascript – 即不支持’包含’方法

javascript – 即不支持’包含’方法

作者:互联网

我一直在研究一个项目并开发一个JavaScript框架.原始代码大约是700行,所以我只粘贴了这一行. include方法在Internet Explorer上不起作用.这有什么解决方案吗?

var row_cells = tbl_row.match(/<td[\s\S]*?<\/td>/g);

    row.Cells = new Array();
    if (onRowBindFuncText != null) { /*Fonksyon tanımlanmaışsa daha hızlı çalış*/

        var cellCount = 0;
        for (i = 0; i < row_cells.length; i++) {

            var cell = new Cell();
            $.each(this, function (k, v) {

                if ((row_cells[i]+"").includes("#Eval(" + k + ")")) {

                    cell.Keys.push(new Key(k,v));

……代码还在继续

解决方法:

因为它在IE中不受支持,所以Opera(see the compatibility table)也不支持它,但您可以使用建议的polyfill

Polyfill

This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can easily polyfill this method:

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

标签:javascript,internet-explorer,ecmascript-7
来源: https://codeday.me/bug/20190926/1818334.html