以下是一个获取当前浏览器 JS 版本的方法:
function getJSVersion() { var jsVersion = ''; if (typeof Map !== 'undefined') { jsVersion = "ES6 or above"; } else if (typeof Symbol !== 'undefined') { jsVersion = "ES2015"; } else if (typeof Proxy !== 'undefined') { jsVersion = "ES6"; } else if (typeof Promise !== 'undefined') { jsVersion = "ES6 Promise"; } else if (typeof Set !== 'undefined') { jsVersion = "ES6 Set"; } else if (!![].entries) { jsVersion = "ES6 Array.from"; } else { // Older browser without any ES6 support jsVersion = "Pre-ES6"; } return jsVersion; } console.log(getJSVersion()); // "ES6 or above" or "ES2015" or "ES6" or "ES6 Promise" or "ES6 Set" or "ES6 Array.from" or "Pre-ES6"
该方法检查 ES6 以上版本的 JavaScript 所添加的新功能的存在。它使用一些这些功能来检查当前浏览器支持的 ES 版本。如果检查成功,它会返回相应的 ES 版本。 否则,它将返回 Pre-ES6
,表示浏览器不支持任何 ES6 和更高版本的 JavaScript 特性。