javascript 如何检测 ECMAscript 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47374678/
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
How to detect ECMAscript version?
提问by Hymany
I want to see what ECMAscript version I'm using in my browser(e.g,chrome 59) ,because there is some difference between ECMAscript3 and ECMAscript5 when dealing with something RegExp stuff.
I've found the relevant information about this,but I can't find a specific answer about how to detect the ECMAscript version.
Thank's in advanced.
我想看看我在浏览器中使用的 ECMAscript 版本(例如,chrome 59),因为在处理 RegExp 内容时,ECMAscript3 和 ECMAscript5 之间存在一些差异。
我已经找到了关于此的相关信息,但我找不到有关如何检测 ECMAscript 版本的具体答案。
先谢谢了。
采纳答案by Nandu Kalidindi
May be you can try to use some data structures that are specifically added in ES6like Map, Setetc. This is to differentiate between ES5and ES6but you can look up for features that are added in ES5which are not present in ES3in your case?
可能您可以尝试使用一些专门添加到ES6like 中的数据结构Map,Set等等。这是为了区分ES5,ES6但是您可以查找添加的功能,而您的案例ES5中不存在这些功能ES3?
try {
var k = new Map();
console.log("ES6 supported!!")
} catch(err) {
console.log("ES6 not supported :(")
}
try {
var k = new HashMap();
console.log("ES100 supported!!")
} catch(err) {
console.log("ES100 not supported :(")
}
回答by Niklas Higi
I don't think that's possible because browsers normally don't implement all features of an ECMAScript version at once. That's why we have libraries like Modernizrand websites like Can I use ...to find out what features can be used.
我认为这是不可能的,因为浏览器通常不会同时实现 ECMAScript 版本的所有功能。这就是为什么我们有像Modernizr这样的库和像Can I use ...这样的网站来找出可以使用哪些功能。
You could still build a little test that determines how RegEx in the user's browser version behaves.
您仍然可以构建一个小测试来确定用户浏览器版本中 RegEx 的行为方式。
回答by themefield
ECMAScript is an industry standard:
ECMAScript 是一个行业标准:
ECMAScript is a trademarked scripting-language specification standardized by Ecma International in ECMA-262 and ISO/IEC 16262. It was created to standardize JavaScript, so as to foster multiple independent implementations.
ECMAScript 是 Ecma International 在 ECMA-262 和 ISO/IEC 16262 中标准化的商标脚本语言规范。创建它是为了标准化 JavaScript,从而促进多个独立的实现。
Javascript is a popular implementation, more or less:
Javascript 或多或少是一种流行的实现:
JavaScript has remained the best-known implementation of ECMAScript since the standard was first published, with other well-known implementations including JScript and ActionScript. wikipedia
自该标准首次发布以来,JavaScript 一直是 ECMAScript 最著名的实现,其他著名的实现包括 JScript 和 ActionScript。维基百科
Considering the current status of JavaScript implementations in various browsers, node.js etc, no one is specifically versioning themselves but keeps evolving with new features. So the best way is to detect, possibly with 3rd party libs, if the feature wanted exists and then use. Otherwise fall back to a more traditional one. Normally it's to try some operations and then check if as expected, no magic.
考虑到各种浏览器、node.js 等中 JavaScript 实现的当前状态,没有人专门对自己进行版本控制,而是随着新功能的不断发展而不断发展。因此,最好的方法是检测(可能使用 3rd 方库)是否存在所需的功能,然后使用。否则回到更传统的。通常是尝试一些操作然后检查是否符合预期,没有魔法。

