Understanding ES5, ES2015 and TypeScript-程序员宅基地

技术标签: Front-end  javascript  

What is the difference between ES5, ES2015 (formerly known as ES6), and TypeScript? Which should we learn and use?

First, let’s create a foundation for our discussion for each of these. TypeScript is a superset of JavaScript. ES2015 is the evolution of ES5. This relationship makes it easier to learn them progressively.

ES5 to ES2015 to TypeScript

We want to understand the differences between them, but first we must understand what each of these are and why they exist. We’ll start with ES5.

ES5

ES5 is what most of us have used for years. Functional programming at its best, or worst, depending on how you view it. I personally love programming with ES5. All modern browsers support it. It’s extremely flexible but also has many factors that contribute to apps that can become train wrecks. Scoping, closures, IIFE’s and good guard logic are required to keep our train on the rails with ES5. Despite this, its flexibility is also a strength that many of us have leaned on.

This chart shows the current compatibility of browsers for ES5.

Perhaps the most difficult problem that ES5 poses for us is the difficulty in identifying issues at development time. Tooling for ES5 is lacking as it is complicated, at best, for a tool to decipher how to inspect ES5 for concerns. We’d like to know what properties an object in another file contains, what an invalid parameter to a function may be, or let us know when we use a variable in an improper scope. ES5 makes these things difficult on developers and on tooling.

ES6/E2015 Leaps Forward

ES2015 is a huge leap forward from ES5. It adds a tremendous amount of functionality to JavaScript. These features address some of the issues that made ES5 programming challenging. They are optional, as we can still use valid ES5 (including functions) in ES2015.

Here are some of the ES2015 features as seen in Luke Hoban’s reference. A full list can be seen here at the spec. - arrows - classes - enhanced object literals - template strings - destructuring - default + rest + spread - let + const - iterators + for..of - generators - unicode - modules - module loaders - map + set + weakmap + weakset - proxies - symbols - subclassable built-ins - promises - math + number + string + array + object APIs - binary and octal literals - reflect api - tail calls

This is a dramatic leap forward to ES5 and modern browsers are racing to implement all of the features. This chart shows the current compatibility of browsers for ES2015.

Node.js is built against modern versions of the V8 engine. Node has implemented much of ES2015, according to its docs.

Node 4.x labels itself as Long Term Support (LTS). The LTS label indicates their release line. All even numbered major versions focus on stability and security. All odd numbered major versions (e.g. 5.x) fall under Short Term Support (STS), which focus on active development and more frequent updates. In short, I recommend you stay on node 4 for production development and node 5 for future research of features that may be in future LTS versions. You can read the official node guidelines for versioning here.

Bringing it back to ES2015, we now have an incredible amount of functionality that we can optionally use to write code.

How Do Developers Consider ES2015?

We might wonder who might be interested in ES2015 and who might not. There are many ES5 developers who are well versed in the pros and cons of the language. After over a decade in JavaScript, we may feel very comfortable with ES5. Once we master a language it can be difficult to justify leaping to a new version if we do not see the value. What are we gaining? What problem are we solving? This is a natural way of thinking. Once we decide if there is value in moving to ES2015, then we can decide to make the move.

There are also many ES5 developers who couldn’t wait to use ES2015. The point is that many folks who have used ES5 are already on to ES2015, while many more are still making that decision to migrate.

There are many JavaScript developers today, but even more are coming. I believe the number now who are considering learning JavaScript and those still on their way, will dwarf that number using it today. JavaScript is growing and not everyone will have had a solid ES5 background. Some are coming from Java and C# and other popular languages and frameworks. Many of these already already have the features that ES2015 recently introduced, and have had them for years. This makes ES2015 a much easier transition for them, than ES5. And it’s good timing too, as many modern browsers and Node are supporting ES2015.

So there are many of us, all with different perspectives, all leading to an eventual ES2015 (or beyond) migration.

Supporting ES5 Browsers

How do we run ES2015 in browsers that do not yet support ES2015? We can use ES2015 and transpile to ES5 using a tool like Babel. Babel makes it easy to write ES2015 (an din the future ES2016 and beyond), and still compile down to an older version of JavaScript. Pretty cool!

TypeScript

Where does TypeScript fit in? Should we even bother with it?

First, I think the name throws people off. The word Type in TypeScript indicates that we now have types. These types are optional, so we do not have to use them. Don;t believe me? Try pasting your ES5 code into the TypeScript playground. Look mom! No types needed! So shouldn’t we optionally call it Type?Script or [Type]Script ? Kidding aside, the types are just once piece of TypeScript. Perhaps a better name is simply ES+.

Let’s step back for a moment and revisit one of the concerns I mentioned previously that many developers have with writing JavaScript: the difficulty in identifying mistakes at development time.

What if we could identify scoping issues as we type them? What if we could identify mismatched parameters in our tool with red underlines? What if our editors and IDEs could tell us when we make a mistake in using the other people’s or our own code improperly? This is what we generally rely on tooling for.

Identifying Issues Early

Whether we use Atom, VS Code, Visual Studio, Web Storm, or Sublime Text we enjoy a plethora of innate features or extensions to our tool of choice that help us write better code faster. These tools should (and can) help use identify problems early.

Is it more fun to find an issue right away as we code it, so we can fix it there … or to get called at 5am due to a production outage when traffic cranked up on our app and hit our hidden bug? I prefer to be home at 5 with my family :)

These tools today try their best to help identify problems, and they do an admirable job with what they have to work with. But what if we could give them a little more help? What if we could give them the same types of help that other languages like C# and Java provide today? Then these tools can really help us identify issues early and often.

This is where TypeScript shines.

The value in TypeScript is not in the writing less code. The value of TypeScript is in writing safer code. Over the long haul, it helps us to write code more efficiently as we take advantage of tooling for identifying issues and automatically filling in parameters, properties, functions, and more (often known as autocomplete and intellisense).

You can try out TypeScript here in their playground.

ES+

I joke that TypeScript should be called ES+, but when we examine it more closely, that is what is really is.

So what does TypeScript offer over ES2015? I’ll focus on the three main additions I feel add the most value:

  1. Types
  2. Interfaces
  3. Future ES2016+ features (such as Annotations/Decorators and async/await)

TypeScript is ES plus features like these.

Types and interfaces help provide the tooling it needs to identify problems early as we type them. With these features our editors don’t have to guess whether we used a function properly or not. The information is readily available for the tool to raise a red flag to us so we can fix he issues right away. In some cases, these tools can also help recommend and refactor for us!

TypeScript promises to be forward thinking. It helps bring the agreed upon features in the future ECMAScript spec to us today. For example features like decorators (used in Angular 2) and async/await (a popular technique to make async programming easier in C#). Decorators are available now in TypeScript while async/await is coming soon in v 2.0 according to the TypeScript roadmap.

Is TypeScript Deviating from JavaScript?

From the top of the TypeScript website’s front page we find this statement:

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

This is hugely important. TypeScript is not a shortcut language. It doesn’t deviate from JavaScript. It doesn’t take us in another direction. It’s purpose is to allow us to use features in the future versions of JavaScript today, and to provide a better and safer experience.

Why Not Just use ES2015?

That’s a great option! Learning ES2015 is a huge leap from ES5. Once you master ES2015, I argue that going from their to TypeScript is a very small step. So I suggest back, once you learn ES2015, try TypeScript and take advantage of its tooling.

What About Employability?

Does learning ES2015 or TypeScript hurt my employability? Absolutely not. But it also doesn’t mean that you shouldn’t understand ES5. ES5 is everywhere today. That will curve down eventually, but there is a lot of ES5 code and it’s good to understand the language both to support it and to understand what problems ES2015 and TypeScript help solve. Plus we can use our knowledge of ES5 to help use debug issues using sourcemaps in the browsers.

Keeping Up with the Emerging Technology

For a long time we didn’t need transpilers. The Web used JavaScript and most folks who wrote in ES3 and ES5 used jQuery to handle any cross browser issues. When ES5 came along, not much changed there. For a long period of years in Web development we had a stable set of JavaScript features that most browsers understood. Where there were issues we used things like es5-shim.js and even jQuery to work around them. Things have changed.

The Web is moving at a fast pace. New Web standards are emerging. Libraries like Angular 2, Rx.js, React, and Aurelia are pushing the Web forward. More developers are coming to JavaScript via the web and Node.js.

The ECMAScript team is now adopting a new name for the language versions using the year as an identifier. No more ES6, now we call it ES2015. The next version is targetted as ES2016. The intention is to drive new features into JavaScript more frequently. It takes time for all browsers to adopt the standards across the desktop and mobile devices.

What does this all mean? Just when we have browsers that support ES2015, ES2016 may be out. Without help, this could be awful if we want to support all ubiquitous browsers and use the new features! Unless we have a way to use the new features today and support the browsers we need.

This is why the emergence of transpilers has become so important in the Web today. TypeScript and Babel (the major players in transpiling) both supported ES2015 before it was in the browsers. They both plan to support (and already do in some cases) ES2016 features. These tools are the current answer to how we move forward without leaving behind our customers.

How Do We Transpile?

We can use tools like Gulp, Grunt, WebPack, and SystemJS with JSPM to transpile with Babel or TypeScript. Many editors connect directly to these tasks to transpile for us as we code. Many IDEs now support automatic transpilation with a click of a button. We can even use TypeScript from the command line to watch our files and transpile as we go.

No matter how or where we code, there are many ways to transpile.

What It All Means

A fact in our chosen profession is that technology changes. It evolves. Sometimes it happens much faster than we can absorb it. That’s why it is important to take advantage of tools that can help us absorb and adapt to the changes, like TypeScript and Babel for ES2015 (and beyond). In this case, we’re using technology to keep up with technology. Seems like a paradox, but at the core it’s simply using our time effectively to keep up.

Reference: https://johnpapa.net/es5-es2015-typescript/

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011153667/article/details/53404325

智能推荐

python实现ElGamal算法_实现elgamal签名算法,并测试签名算法和验证算法的时间开销python代码-程序员宅基地

文章浏览阅读504次,点赞14次,收藏5次。ElGamal公钥密码算法是在密码协议中有着重要应用的一类公钥密码算法,基于公钥密码体制和椭圆曲线加密体系,其安全性是基于有限域上离散对数学问题的难解性。至今仍是一个安全性良好的公钥密码算法。Alice和Bob生成各自的密钥,并交换彼此的公钥(p,g,y),Alice和Bob之间约定签署的消息m(20240301),Alice签署消息m,将三元组(m,r,s)发给Bob,Bob验证签名。这种数据或变换允许数据单元的接收者用以确认数据单元的来源和数据单元的完整性并保护数据,防止被人(例如接收者)进行伪造。_实现elgamal签名算法,并测试签名算法和验证算法的时间开销python代码

【解决】React setState延迟delay导致数据更新不及时,代码无法正确执行-程序员宅基地

文章浏览阅读7.2k次。之前React setState后在另一个函数调用state的值没有更新: // init state.type = 'new';activeMenu(type) { // type = top if (type == this.state.type) return; this.loadList(type);}loadList(type) { ..._react delay

常见的SQL优化面试题-程序员宅基地

文章浏览阅读6.4k次,点赞16次,收藏82次。1.在表中建立索引,优先考虑 where group by 使用到的字段2.查询时尽量避免使用select * ,只查询需要用到的字段3.避免在where子句中使用关键字两边都是%的模糊查询,尽量在关键字后使用模糊查询4.尽量避免在where子句中使用IN 和NOT IN优化:能使用between就不用in在子查询中使用exists 子句5.尽量避免使用or,优化:可以用union代替..._sql优化面试题

使用idea工具原型创建java项目以及web项目(Maven)_idea2023创建新项目有个原型是什么意思-程序员宅基地

文章浏览阅读813次。我们这个工程一定要进行Maven的配置具体的过程在下面的这篇文章中,只需要看前半部分就可以http://t.csdn.cn/ADnVy首先来看创建java 项目(哪里亮了点哪里)第一步:第二步:第三步: 记得勾选Create...然后选择我们选中的这个模板,点击next第四步:选择好文件的路径以及命名第五步:选择好Maven版本,已经仓库,这个我们在文章一开始的连接中有介绍,完成后点击Finish第六步:就会出......_idea2023创建新项目有个原型是什么意思

架构师聊的四层代理和七层代理,都在聊什么?_ttl七层代理-程序员宅基地

文章浏览阅读2.8k次,点赞3次,收藏13次。日常与大佬沟通或看文章,时不时总会遇到两个概念“四层代理负载均衡”和“七层代理负载均衡”,那么,所谓的四层代理和七层代理分别指的是什么?又在什么场景下用到呢?这篇文章就带大家聊聊这方面的知识点。OSI七层模型要聊几层代理,需要先看一下网络分层,在之前的文章中也提到,标准的七层网络分层,也就是OSI七层模型。TCP/IP五层模型和TCP/IP四层模型是从OSI七层优化而来。这里所谈的四层代理和七层代理,便是基于OSI七层模型来划分的。从下往上看,第四层为传输层、第七层为应用层。再来看看每层对应的常见_ttl七层代理

计算机无法连接蓝牙键盘,终于理会电脑无法识别蓝牙鼠标键盘-程序员宅基地

文章浏览阅读7.1k次。电脑无法识别蓝牙鼠标键盘那该怎么办?有时候我们在使用U盘或者鼠标的时候,也许会出现一些故障,今天汇学小编就这话题给大家说说相关方法。方法一:我们先打开电脑,然后点击左下角的那个圆形的开始,我们点击进入到主页面即可。然后我们可以看到的页面,我们在这里找到图中框线处的控制面板,点击进入然后我们在控制面板的主页面里选择“硬件和声音”,然后点击进入主页面即可。然后我们在其中找到添加蓝牙设备,如图框线处所示..._电脑连接蓝牙键盘时检测不到

随便推点

latex 参考文献显示问号_LaTeX入门(1)-程序员宅基地

文章浏览阅读1.4k次。又是一年的论文季,作为数学系的秃鹫,只会用word排版论文是远远不够的,接下来我将这几天的学习收获分享给大家,也是对自己实战能力的一种考验,这篇文章基本不会告诉你们LaTeX和word相比的各种优势,大家可以参考网上的其他说法,我只会告诉你们,用LaTeX就完事了!LaTeX抛弃了word中传统的“所见即所得”的思想,它不注重文章的排版,样式,而是让作者更加专注于自己所写文章的内容、层次结构,而且..._latex插入参考文献后为问好

【毕业设计】基于SSM的OA办公管理系统的设计与实现 -java web_基于xx技术的某公司oa人事管理系统设计和实现-程序员宅基地

文章浏览阅读945次。Hi,同学们好呀,学长今天带大家复盘一个学长帮往届同学做的一个毕业作品基于Java web的OA系统的设计与实现办公自动化简称为OA(Office Automation)是集计算机科学、通讯技术、系统科学、行为科学为一体的综合性技术。现代的办公自动化系统采用的是Internet/Intranet技术,基于工作流的概念,使得企业内部的员工能够方便快捷地共享信息,高效地协同工作;改变传统复杂、低效的手工办公方式,以实现迅速、全方位的信息采集和信息处理,为企业的管理和决策提供科学的依据。_基于xx技术的某公司oa人事管理系统设计和实现

deepin/UOS自带浏览器安装插件_uos浏览器插件-程序员宅基地

文章浏览阅读2k次。deepin/UOS自带浏览器安装插件_uos浏览器插件

128.Django应用Celery_django celery-程序员宅基地

文章浏览阅读1.2k次。在Django中使用处理异步任务框架Celery_django celery

Win11策略服务被禁用怎么办?Win11策略服务被禁用的解决方法_根据策略禁用了文件传输-程序员宅基地

文章浏览阅读1.6k次。​相信很多小伙伴都喜欢到组策略服务器上去对电脑进行各种高级设置,不过最近一些使用Win11系统的小伙伴反馈说自己在的策略服务出现了被禁用情况,那么遇到这种问题应该怎么办呢?下面就和小编一起来看看策略服务被禁用的解决方法吧。还有详细的一键重装系统方法  1、首先使用键盘快捷键“Win+R”打开运行。  2、打开后输入“gpedit.msc”回车打开组策略。  3、打开后,进入左边“管理模板”下的“系统”。  4、再打开右边“系统还原”。  5、随后双击打开“关闭系统还原”。  6、最后将他设置为“已禁用”并确_根据策略禁用了文件传输

树莓派ROS stm32 slam Freertos VFH+A*避障路径规划-智能平衡计划(五)_rtos “slam”-程序员宅基地

文章浏览阅读1.2k次。基于树莓派ROSstm32搭载Freertos智能平衡车Day5前言1.引入库2.读入数据总结前言用超声波先避个障,日后改成激光雷达。# 一、GPIO配置需要配置一个定时器,一个外部中断(上升沿和下降沿都可以触发)给脉冲触发引脚(trig)输入一个长为20us的高电平方波输入方波后,模块会自动发射8个40KHz的声波,与此同时回波引脚(echo)端的电平会由0变为1;(此时应该启动定时器计时)3.当超声波返回被模块接收到时,回波引 脚端的电平会由1变为0;(此时应该停止定时器计数),_rtos “slam”

推荐文章

热门文章

相关标签