EcmaScript-6向后兼容 [英] EcmaScript-6 backward compatibility

查看:120
本文介绍了EcmaScript-6向后兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道/弄清楚ECMAScript-6新变化是否适用于旧浏览器。

I am curious to understand/figure-out if the ECMAScript-6 new-changes will work on the old browsers or not.

我为什么提出这个问题是:

我记得在ECMAScript-中引入了'use strict'; 5,它是为了与旧版本兼容。

I remember the introduction of 'use strict'; in ECMAScript-5, it was meant for the compatibility with the old versions.

这意味着旧的浏览器会保持正常工作,当他们遇到'use strict'时会忽略它; 解析新的JavaScript代码时的语句。

That means the old browsers will keep working fine and they will just ignore it when they encounter the 'use strict'; statement while parsing the new JavaScript code.

新的JS引擎将处理语句'use strict'; 以一些特殊方式详细说明严格模式

And the new JS-engines will treat the statement 'use strict'; in some special way as detailed here Strict mode.

所以,回答问题

I非常怀疑和好奇知道ECMAScript-5兼容的浏览器在解析ECMAScript-6代码时会如何表现。

I seriously doubt and curious to know how would the ECMAScript-5 compliant browsers behave when they will parse the ECMAScript-6 code.

我怀疑的原因是ECMAScript-6 new功能涉及语法更改/更新。旧的浏览器 new-syntax-unaware-engines 会在遇到以下任何新语法时开始抛出错误

The reason for my doubt is ECMAScript-6 new features involve syntax change/updates. And the old browsers which are new-syntax-unaware-engines will start throwing errors when they encounter any of the new syntax from the following

yield [*],Map,Set,WeakMap,function * foo(){},=>,等等...

我关心的是ECMAScript-6中的新功能的决定/包含是否支持旧浏览器而没有任何代码中断?

My concern is has the decision/inclusion of new features in ECMAScript-6 taken care of supporting the old-browsers without any break of code?

如果是那么如何?

如果不是那我该怎么办才能让我的老浏览器用户高兴?

If Not then what should I do to keep my old-browser-users happy?

我看到一个解决方案,通过包含一些 transpiler ,例如我项目中的 traceur-compiler 。这会将我的ECMAScript-6代码转换为ECMAScript-5等效代码。但是,我还有其他任何解决办法让我的旧浏览器用户高兴吗?

I see one solution to keep the users using old browsers happy by including some transpiler like traceur-compiler in my project. This will convert my ECMAScript-6 code to ECMAScript-5 equivalent. But do I have any other solution to keep my old-browser-users happy?

推荐答案

许多ES6功能在ES5 JS引擎中不起作用,特别是新的语法功能,例如 for / of 或箭头函数,生成器等....一些功能,如Set对象可以为旧浏览器部分填充,其他人则不能。

Many ES6 features will not work in an ES5 JS engine, particularly new syntax features such as for/of or arrow functions, generators, etc.... Some features like the Set object can be partially polyfilled for older browsers, others cannot.

您在问题中的功能列表:

Of the list of features you had in your question:

yield[*], Map, Set, WeakMap, function* foo(){}, =>, for...of

这些都与旧版本的Javascript兼容,并且会导致语法或参考错误。 Map Set 的某些特征可以是polyfilled(尽管不是全部)。产量,生成器,箭头函数和for ...只是旧浏览器不处理且无法执行的新语法。可以使用ES6转换器将代码转换为ES5兼容代码。这不是真正的ES6向后兼容性,而是仅使用ES5语法来完成与新ES6语法中表达的相同内容的代码转换。其中一些是使用polyfill完成的,有些是使用ES5代码表示ES6构造的替代方法(通常是更多的ES5代码)。

None of those are compatible with older versions of Javascript and will either cause syntax or reference errors. Some characteristics of Map and Set can be polyfilled (though not all). Yield, generators, arrow functions and for...of are just new syntax that older browsers do not process and cannot execute. It is possible to use an ES6 transpiler that will convert your code to ES5-compatible code. That isn't really ES6 backwards compatibility, but rather a code conversion that uses only ES5 syntax to accomplish the same things that are expressed in the newer ES6 syntax. Some of that is done with polyfills and some done with alternative ways of expressing an ES6 construct using only ES5 code (and usually more ES5 code).

如果您的代码运行在类似于node.js或者如果它是特定浏览器的特定版本的插件,那么您可以更好地控制JS引擎,并且可能比在浏览器中更快地使用ES6功能。

If your code runs in something like node.js or if it's a plug-in for a specific version of a specific browser, then you have better control over the JS engine and can likely use ES6 features sooner than in a browser.

如果您的代码在浏览器中运行而您没有使用转换器转换为ES5代码,那么在使用大多数浏览器之前,这将是一段时间(很多年)在互联网上都是ES6准备就绪。

If your code runs in a browser and you're not using a transpiler to conver to ES5 code, then it will be awhile (many years) until most browsers in use on the internet are all ES6 ready.

的不同目的使用严格; (删除支持不良实践)与允许与旧版本兼容而不是像生成器这样的新语言功能更加一致,因为use strict; 构造被特别选择为新浏览器可以检测,但较旧的浏览器只会看到正常的字符串。代表新语言语法的新ES6功能根本不是那种方式,因为旧浏览器不知道如何处理它们,即使它们可能以某种方式忽略新语法,它们也不支持新语法所暗示的功能。

The different purpose of "use strict"; (removing support for bad practices) is more consistent with allowing for compatibility with older versions than new language features like generators as the "use strict"; construct was specifically chosen to be something that a new browser could detect, but an older browser would just see as a normal string. New ES6 features that represent new language syntax are simply not that way as older browsers don't know how to process them and even if they could somehow ignore the newer syntax, they don't support the functionality that the new syntax implies.

您可能会发现这篇文章非常有用,它讨论了今天尝试使用ES6的一些问题:

You may find this article useful which discusses some of the issues in trying to use ES6 today:

ECMAScript 6用于好奇JavaScripter的资源

如果你想在各种浏览器中使用大多数ES6功能,那么最好的选择可能就是使用像 BabelJS 。这会将您的ES6代码转换为可在任何ES5浏览器中运行的ES5兼容代码。您可以在ES6中编写代码,但代码将在各种浏览器中运行。

If you want to use most of ES6 capabilities today in a wide range of browsers, then your best option is probably to transpile your code using something like BabelJS. This will transpile your ES6 code into ES5 compatible code that will run in any ES5 browser. You get to write in ES6, but the code will run in a wide range of browsers.

或者,如果您只在特定环境中运行(例如该浏览器的特定版本的浏览器插件或特定的运行时引擎(如node.js),然后您可以编写使用该特定引擎已经支持的ES6功能的代码。

Or, if you're running in only a specific environment (such as a browser plug-in for a specific version of that browser) or a specific runtime engine such as node.js, then you can write code that uses the ES6 features that are already supported in that specific engine.

这篇关于EcmaScript-6向后兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆