Home > Common Problem > body text

Do all browsers now support es6?

青灯夜游
Release: 2022-10-20 14:20:20
Original
4985 people have browsed it

No; ES6 provides many new features, but not all browsers can perfectly support it. For example, IE7~11 versions basically do not support ES6, and Edge12-14 only supports some of the new ES6 features; for ES6 The most user-friendly browsers with new features are Chrome and Firefox. Chrome can support 97% of ES6 new features starting from version 51, and Firefox can support 97% of ES6 new features starting from version 53.

Do all browsers now support es6?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

ES6 provides many new features, but not all browsers can fully support them. Fortunately, major browsers are now accelerating their compatibility with the new features of ES6. Among them, the most friendly browsers for the new features of ES6 are Chrome and Firefox.

Introduction to browser compatibility of ES6 syntax

##12-1415-18 ,79-87Firefox2-56-5354-86Chrome4-2021-5051-90Safari3.1-77.1-9.110-13.1, 14, TP##Opera##iOS Safari7-9.310-13.7, 14.2Opera Mini##Android Browser12-12.1Chrome for Android##83Samsung Internet45-13.0 ##10.4##7.12##

For details on each browser’s support for ES6, please check https://caniuse.com/?search=es6

If you want to know whether your browser supports ES6, please check http:// ruanyf.github.io/es-checker/index.cn.html

Desktop browser support for ES2015

  • Chrome: Starting from version 51, it can support 97% of the new features of ES6.

  • Firefox: Supports 97% of ES6 new features starting from version 53.

  • Safari: Supports 99% of ES6 new features starting from version 10.

  • IE: Edge 15 can support 96% of the new ES6 features.

  • Edge 14 can support 93% of the new ES6 features. (IE7~11 basically does not support ES6)

It can be seen that IE11 is dragging its feet again, completely giving up on ES6, and Edge will support its future.

IE11 is effectively compatible with ES6

So how do you make pure ES6 scripts run under IE11? Babel provides an effective solution.

Introduce two scripts:

https://cdn.bootcss.com/babel-core/5.8.35/browser.min.js

The The script converts statements at the es6 syntax level

https://cdn.bootcss.com/babel-core/5.8.35/browser-polyfill.min.js

This script converts the new syntax API, such as Set Map Promise, etc. methods

Mark the script blocktype = "text/babel"

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IE11 With ES6</title>
    <script src="./browser-polyfill.min.js"></script>
    <script src="./browser.min.js"></script>
    <script type="text/babel">
        const list = [&#39;one&#39;, &#39;two&#39;, &#39;three&#39;];
        list.forEach((item, index) => {
            alert(item + (index + 1));
        });


        let promise = new Promise(function (resolve, reject) {
            alert(&#39;Promise&#39;);
            resolve();
        });

        promise.then(function () {
            alert(&#39;resolved.&#39;);
        });


        const items = new Set([1, 2, 3, 4, 5, 5, 5, 5]);
        alert(items.size)


        const map = new Map();

        const k1 = [&#39;a&#39;];
        const k2 = [&#39;a&#39;];

        map.set(k1, 111).set(k2, 222);

        alert(map.get(k2))

    </script>
</head>

<body>

</body>

</html>
Copy after login

So there are two confusions here:

First: <script type="text/babel"> and the &lt we usually use ;script type="text/javascript">What is the difference.

Second: What exactly does polyfill do?

Let’s explain it separately. Let’s look at an uncompressed code: https://cdn.bootcss.com/babel-core/5.8 .38/browser.js
Found the following code snippets:

//页面加载后,执行runScripts方法
if (global.addEventListener) {
  global.addEventListener("DOMContentLoaded", runScripts, false);
} else if (global.attachEvent) {
  global.attachEvent("onload", runScripts);
}
Copy after login
var runScripts = function runScripts() {
  var scripts = [];
  //识别类型
  var types = ["text/ecmascript-6", "text/6to5", "text/babel", "module"];
  var index = 0;

  /**
   * Transform and execute script. Ensures correct load order.
   */

  var exec = function exec() {
    var param = scripts[index];
    if (param instanceof Array) {
      transform.run.apply(transform, param);
      index++;
      exec();
    }
  };

  /**
   * Load, transform, and execute all scripts.
   */

  var run = function run(script, i) {
    var opts = {};

    if (script.src) {
      transform.load(script.src, function (param) {
        scripts[i] = param;
        exec();
      }, opts, true);
    } else {
      opts.filename = "embedded";
      scripts[i] = [script.innerHTML, opts];
    }
  };

  // Collect scripts with Babel `types`.

  var _scripts = global.document.getElementsByTagName("script");
  //按照类别加载
  for (var i = 0; i < _scripts.length; ++i) {
    var _script = _scripts[i];
    if (types.indexOf(_script.type) >= 0) scripts.push(_script);
  }
  //执行
  for (i in scripts) {
    run(scripts[i], i);
  }

  exec();
};
Copy after login

I think the text/babel we are paying attention to is here: var types = ["text/ecmascript-6", "text/ 6to5", "text/babel", "module"];

Get the steps marked with the above three items on the page, and then use the transform library to load and translate them into ES5 for execution.

So what does polyfill do? Continue reading the code https://cdn.bootcss.com/babel-core/5.8.38/browser-polyfill.js
Also locate a piece of code:

$export($export.P, &#39;Array&#39;, {
  // 22.1.3.10 / 15.4.4.18 Array.prototype.forEach(callbackfn [, thisArg])
  forEach: $.each = $.each || methodize(createArrayMethod(0)),
  // 22.1.3.15 / 15.4.4.19 Array.prototype.map(callbackfn [, thisArg])
  map: methodize(createArrayMethod(1)),
  // 22.1.3.7 / 15.4.4.20 Array.prototype.filter(callbackfn [, thisArg])
  filter: methodize(createArrayMethod(2)),
  // 22.1.3.23 / 15.4.4.17 Array.prototype.some(callbackfn [, thisArg])
  some: methodize(createArrayMethod(3)),
  // 22.1.3.5 / 15.4.4.16 Array.prototype.every(callbackfn [, thisArg])
  every: methodize(createArrayMethod(4)),
  // 22.1.3.18 / 15.4.4.21 Array.prototype.reduce(callbackfn [, initialValue])
  reduce: createArrayReduce(false),
  // 22.1.3.19 / 15.4.4.22 Array.prototype.reduceRight(callbackfn [, initialValue])
  reduceRight: createArrayReduce(true),
  // 22.1.3.11 / 15.4.4.14 Array.prototype.indexOf(searchElement [, fromIndex])
  indexOf: methodize(arrayIndexOf),
  // 22.1.3.14 / 15.4.4.15 Array.prototype.lastIndexOf(searchElement [, fromIndex])
  lastIndexOf: function(el, fromIndex /* = @[*-1] */){
    var O      = toIObject(this)
      , length = toLength(O.length)
      , index  = length - 1;
    if(arguments.length > 1)index = Math.min(index, toInteger(fromIndex));
    if(index < 0)index = toLength(length + index);
    for(;index >= 0; index--)if(index in O)if(O[index] === el)return index;
    return -1;
  }
});


var createArrayReduce = function(isRight){
  return function(callbackfn, memo){
    aFunction(callbackfn);
    var O      = IObject(this)
      , length = toLength(O.length)
      , index  = isRight ? length - 1 : 0
      , i      = isRight ? -1 : 1;
    if(arguments.length < 2)for(;;){
      if(index in O){
        memo = O[index];
        index += i;
        break;
      }
      index += i;
      if(isRight ? index < 0 : length <= index){
        throw TypeError(&#39;Reduce of empty array with no initial value&#39;);
      }
    }
    for(;isRight ? index >= 0 : length > index; index += i)if(index in O){
      memo = callbackfn(memo, O[index], index, this);
    }
    return memo;
  };
};
Copy after login

You can find that ployfill adds many new methods to Arrary, such as createArrayReduce, which is used to implement reduce.

Note

Introducing the above two files basically solves most of the browser's support for ES6.

But again: Even if you use a conversion tool, it is not recommended to use a large number of new features that have low browser support for ES6 in a production environment. After all, this is performed after online conversion, and the efficiency is relatively low. Moreover, as browser support for ES6 changes, these conversion scripts also need to be updated frequently, which will inevitably have an impact on later maintenance.

【Related recommendations: javascript video tutorial, programming video

Browser Unsupported version Partially supported versions Supported versions
IE 6-10 11
Edge
10-12.1 15-37 38-72
3.2-6.1
all

2.1-4.34.4-4.4 .4 81 ##Opera Mobile
59
##87

Firefox for Android

UC Browser for Android
##12.12


QQ Browser

Baidu Browser

KaiOS Browser
2.5

The above is the detailed content of Do all browsers now support es6?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!