How to detect ECMAscript version?

2019-04-23 00:22发布

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.

3条回答
甜甜的少女心
2楼-- · 2019-04-23 00:25

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 Modernizr and websites like Can I use ... to find out what features can be used.

You could still build a little test that determines how RegEx in the user's browser version behaves.

查看更多
做自己的国王
3楼-- · 2019-04-23 00:28

ECMAScript is an industry standard:

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.

Javascript is a popular implementation, more or less:

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

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.

查看更多
叼着烟拽天下
4楼-- · 2019-04-23 00:43

May be you can try to use some data structures that are specifically added in ES6 like Map, Set etc. This is to differentiate between ES5 and ES6 but you can look up for features that are added in ES5 which are not present in ES3 in your case?

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 :(")
}

查看更多
登录 后发表回答