为什么在浏览器中使用ES6 [英] Why use ES6 in browsers

查看:91
本文介绍了为什么在浏览器中使用ES6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node.js项目,我在服务器上使用ES6语法,在浏览器端使用非ES6语法。我也尝试在浏览器上使用ES6语法,但是当我访问iPhone上的网站时遇到问题,所以我切换回'旧的javascript语法'。

I have a Node.js project in which I use ES6 syntax on the server and non-ES6 syntax on the browser side. I have also tried using ES6 syntax on the browser, but then I experienced problems when I visited the website on iPhones, so I switched back to 'old javascript syntax'.

我可以从Github上的Bootstrap代码中看到他们在Bootstrap v4的代码中使用ES6(const,箭头函数等)。

I can, from Bootstrap's code on Github, see that they use ES6 (const, arrow functions, etc.) in the code for Bootstrap v4.

它是否巧妙避免使用ES6语法,直到所有使用的浏览器都支持它?我假设总会有一些浏览器被使用,不支持新语法,那么为什么有人敢于转向新语法呢?我知道它会很少,但由于ES6可以实现的所有功能在没有ES6的情况下也是可能的,我不知道如何使用新语法来降低我网站上潜在客户的损失。

Is it clever to avoid using ES6 syntax until all used browsers support it? I assume that there always will be a few browsers being used that do not support the new syntax, so why would anyone ever dare to move to the new syntax? I know that it will be very few, but since all the stuff that is possible with ES6 is also possible without ES6, I do not see how it can be worth using the new syntax for the cost of losing potential customers on my website.

是否存在一些脚本可以确保所有访问者的浏览器都支持ES6?

Do there exist some scripts that can make sure all my visitors' browsers support ES6?

推荐答案


在所有使用的浏览器都支持ES6语法之前,它是否聪明?

Is it clever to avoid using ES6 syntax until all used browsers support it?

不是真的。您可以将其转换为例如 babel (这就是您提出的Bootstrap v4,也有)。

Not really. You can transpile it, for instance with babel (that's what Bootstrap v4, which you raise as an example, also does).

事实上,即使你保证使用现代JS运行时,你也可能希望与babel一起使用,使用尚未完全标准化的语言功能。重点不是新的用户可见功能,而是程序员的工作效率,您可以通过ES6以多种方式提升。

In fact you may want to transpile with babel even if you are guaranteed to have a modern JS runtime in use, to use not-yet-even-quite-standardised language features. The point is not new user visible features but programmer productivity which you can boost in many ways with ES6.

您将找到有关如何使用EST转换浏览器预期代码的明确说明babel 例如在此博文中

You'll find clear instructions of how to transpile browser intended code with babel for instance in this blog post.


因为ES6可能的所有东西也可以在没有ES6的情况下使用,我不知道如何使用新语法来获得成本在我的网站上失去潜在客户。

since all the stuff that is possible with ES6 is also possible without ES6, I do not see how it can be worth using the new syntax for the cost of losing potential customers on my website.

在撰写本文时,转换是一个非常重要的步骤,除非你知道你正在写一篇文章。特定的JS执行环境(例如基于已知版本的WebKit / JavaScriptCore的混合Web浏览器/桌面或移动应用程序)。

At the time of writing transpiling is a pretty essential step, unless you know you are writing against a specific JS execution environment (for instance a hybrid web browser / desktop or mobile app that is based on a known version of WebKit / JavaScriptCore).

对于现在几乎所有现代浏览器中存在的许多API,使用隐藏平台差异的抽象多年来也是必不可少的,特别是(但不限于)旧版本的IE,因此,如果您的目的旧浏览器版本很重要,那么您将很长一段时间。但是,不应该阻止你获得生产力的好处!

Using abstractions that hide platform differences has been essential for years also for many APIs now present in practically all modern browsers, especially (but not limited to) older versions of IE, so you'll be transpiling a long time still if for your purposes old browser versions matter. Shouldn't stop you from getting the productivity benefits, though!

这篇关于为什么在浏览器中使用ES6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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