Javascript 即不支持“包含”方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31221341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:24:04  来源:igfitidea点击:

ie does not support 'includes' method

javascriptinternet-explorerecmascript-7

提问by Onur Emrecan ?zcan

I have been working on a project and developing a JavaScript framework. The original code is about 700 lines so I only pasted this line. The includes method doesn't work on Internet Explorer. Is there any solution for this?

我一直在做一个项目并开发一个 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));

...Code goes on

...代码继续

回答by alessandro

Because it's not supported in IE, it is not supported also in Opera (see the compatibility table), but you can use the suggested polyfill:

因为 IE 不支持它,Opera 也不支持它(请参阅兼容性表),但您可以使用建议的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:

填充物

此方法已添加到 ECMAScript 2015 规范中,可能尚未在所有 JavaScript 实现中可用。但是,您可以轻松地填充此方法:

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;
    }
  };
}

回答by Sunho Hong

@Infer-on shown great answer, but it has a problem in a specific situation. If you use for-in loop it will return includes "includes" function you added.

@Infer-on 给出了很好的答案,但它在特定情况下有问题。如果您使用 for-in 循环,它将返回您添加的包含“包含”功能。

Here is another pollyfill.

这是另一个pollyfill。

if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, "includes", {
    enumerable: false,
    value: function(obj) {
        var newArr = this.filter(function(el) {
          return el == obj;
        });
        return newArr.length > 0;
      }
  });
}

回答by Patrick Duncan

You could just use .search() > -1 which behaves in the exact same way. http://www.w3schools.com/jsref/jsref_search.asp

您可以使用 .search() > -1 ,其行为方式完全相同。 http://www.w3schools.com/jsref/jsref_search.asp

if ((row_cells[i]+"").search("#Eval(" + k + ")") > -1) {

回答by patrickbadley

This Selected answer is for String, if you are looking for 'includes' on an array, I resolved my issue by adding the following to my polyfills.ts file:

This Selected answer is for String,如果您正在寻找数组上的“包含”,我通过将以下内容添加到我的 polyfills.ts 文件来解决我的问题:

import 'core-js/es7/array';

回答by mvermand

This is a polyfill for TypeScript projects, taken from https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/includesand modified to be valid TypeScript:

这是 TypeScript 项目的 polyfill,取自https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/includes并修改为有效的 TypeScript:

if (!Array.prototype.includes) {
    Object.defineProperty(Array.prototype, 'includes', {
        value: function(searchElement, fromIndex) {

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

            const o = Object(this);
            // tslint:disable-next-line:no-bitwise
            const len = o.length >>> 0;

            if (len === 0) {
                return false;
            }
            // tslint:disable-next-line:no-bitwise
            const n = fromIndex | 0;
            let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

            while (k < len) {
                if (o[k] === searchElement) {
                    return true;
                }
                k++;
            }
            return false;
        }
    });
}

回答by user11374562

if (fullString.indexOf("partString") >= 0) {
//true 

} else {
//false
}

回答by mohan mu

var includes = function(val, str) {
  return str.indexOf(val) >= 0;
};