| ##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 = ['one', 'two', 'three'];
list.forEach((item, index) => {
alert(item + (index + 1));
});
let promise = new Promise(function (resolve, reject) {
alert('Promise');
resolve();
});
promise.then(function () {
alert('resolved.');
});
const items = new Set([1, 2, 3, 4, 5, 5, 5, 5]);
alert(items.size)
const map = new Map();
const k1 = ['a'];
const k2 = ['a'];
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 < 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, 'Array', {
// 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('Reduce of empty array with no initial value');
}
}
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】