by Preethi Kasireddy

通过Preethi Kasireddy

为什么要在JavaScript中使用静态类型? 我们是否应该使用它们? (Why use static types in JavaScript? Should we use them or not?)

Note: This is Part 3 of a 3-part series. You can check out Part 1 and Part 2 if you haven’t already!

注意:这是3部分系列的第3部分。 您还可以查看第1部分和第2部分 !

那么我们是否应该在JavaScript中使用静态类型? (So should we use static types in JavaScript or not?)

The first programming languages I learned were JavaScript and Python, both of which are dynamically-typed languages.

我学习的第一种编程语言是JavaScript和Python,它们都是动态类型化的语言。

But my foray into static types have added a whole new dimension to how I think about programming. For example, even though I found the Elm compiler errors overwhelming at first, defining types and “pleasing the compiler” became second nature, and actually improved my code thinking. Plus, there’s nothing more liberating than an intelligent robot telling me when I’m doing something wrong and how to fix it.

但是我对静态类型的尝试为我对编程的思考增加了一个全新的维度。 例如,尽管我一开始发现Elm编译器错误不堪重负,但定义类型和“取悦编译器”成为第二天性,实际上改善了我的代码思维。 另外,没有什么比解放智能的机器人告诉我什么时候做错了以及如何解决它要解放了。

Despite the tradeoffs that come with types like verbosity and the upfront investment to master them, the safety and correctness that types add to our programs make these “disadvantages” less of an issue for me personally.

尽管诸如冗长性和要掌握它们的前期投资之类的类型需要权衡取舍,但类型为我们的程序增加的安全性和正确性使这些“缺点”对我个人而言不再是一个问题。

Dynamic typing feels faster and easier, but it sometimes loses ground once you actually try to make a program run in the wild. At the same time, you can talk to any Java developer who’s had to work with more complicated generic type definitions and they’ll tell you all about how much they hate types.

动态类型输入感觉更快,更容易,但是一旦您实际尝试使程序在野外运行,它有时会失去作用。 同时,您可以与必须处理更复杂的泛型类型定义的任何Java开发人员进行交谈,他们会告诉您所有讨厌类型的信息。

Ultimately, there’s no silver bullet. My personal approach is to favor using types under these circumstances:

最终,没有灵丹妙药。 我个人的方法是在以下情况下使用类型:

  1. The program is critical to your business该计划对您的业务至关重要
  2. The program is likely to be refactored as your needs evolve该程序可能会随着您的需求发展而重构
  3. The program is complex and has many moving parts该程序很复杂,并且有很多活动部分
  4. The program is maintained by a large team of developers who need to be able to grasp and understand the code quickly and accurately该程序由庞大的开发人员团队维护,他们需要能够快速,准确地掌握和理解代码

On the flip side, I’d consider opting out of types in these types of situations:

另一方面,我会考虑在以下几种情况下选择退出类型:

  1. The code is short-lived and non-critical该代码是短暂且非关键的
  2. You’re prototyping and trying to move as quickly as possible您正在制作原型并试图尽快移动
  3. The program is small and/or simple该程序很小和/或简单
  4. You’re the only developer您是唯一的开发者

The beauty of being a JavaScript developer today is that because of tools like Flow and TypeScript, we finally have a choice of whether to use static types or good ol’ vanilla JavaScript.

如今,成为JavaScript开发人员的好处在于,由于使用了Flow和TypeScript之类的工具,我们最终可以选择使用静态类型还是使用优秀的普通JavaScript。

结论 (Conclusion)

I hope this post helped you get a feel for why types matter, how to use them, and most importantly *when* to use them.

我希望这篇文章可以帮助您了解为什么类型重要,如何使用它们,最重要的是*何时*使用它们。

Being able to toggle between dynamic and static types is a powerful tool for the JavaScript community — and exciting :)

能够在动态和静态类型之间切换是JavaScript社区的强大工具-令人兴奋的是:)

More questions? As always, ping me in the comments to keep the conversation going.

还有其他问题吗? 与往常一样,在评论中回复我以保持对话的进行。

翻译自: https://www.freecodecamp.org/news/why-use-static-types-in-javascript-part-4-b2e1e06a67c9/

为什么要在JavaScript中使用静态类型? 我们是否应该使用它们?相关推荐

  1. 为什么要在JavaScript中使用静态类型? (使用Flow进行静态打字的4部分入门)

    by Preethi Kasireddy 通过Preethi Kasireddy 为什么要在JavaScript中使用静态类型? (使用Flow进行静态打字的4部分入门) (Why use stati ...

  2. javascript优缺点_为什么要在JavaScript中使用静态类型? 优缺点

    javascript优缺点 by Preethi Kasireddy 通过Preethi Kasireddy 为什么要在JavaScript中使用静态类型? 优缺点 (Why use static t ...

  3. 创建健壮的isArray()函数(JavaScript中判断对象类型的种种方法)

    我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...

  4. 解析JavaScript中的字符串类型与字符编码支持

    JavaScript中的字符串也像Python那样支持反斜杠的转移,并且字符集方面默认为Unicode,下面就来详细解析JavaScript中的字符串类型与字符编码支持 定义 字符串就是零个或多个排在 ...

  5. Vue中的静态类型检查

    Vue还利用Javascript ES6语法来编写代码.然而,当涉及到静态类型检查时,在Vue中使用Typescript就不是那么简单了.有一些课程是关于如何将Typescript和Vue一起使用的, ...

  6. javascript中对变量类型的判断

    在JavaScript中,有5种基本数据类型和1种复杂数据类型,基本数据类型有:Undefined,Null,Boolean, Number和String:复杂数据类型是Object,Object中还 ...

  7. javascript中对变量类型的推断

    本文正式地址:http://www.xiabingbao.com/javascript/2015/07/04/javascript-type 在JavaScript中,有5种基本数据类型和1种复杂数据 ...

  8. javascript中的Date类型

    1.JavaScript中的Date表示日期时间. 2.new Date()返回日期对象,是本地时间 3.Date.parse()接受一个时间字符串返回时间戳.也是本地时间,所以Date.parse( ...

  9. JavaScript中的强制类型转换规则

    JavaScript中的类型从来都不是在定义的时候就确定的,而是在运行期间确定的 JavaScript中的强制类型转换总是返回基本类型值 JavaScript中常见的转换就是向数字.字符串.布尔值进行 ...

最新文章

  1. android_launcher的源码详细分析和壁纸修改 .
  2. Firefox UI已迁移至Web Components
  3. [转载] 中国象棋软件-引擎实现(二)棋局表示
  4. PTA—考试座位号(C语言)
  5. 拓端tecdat|Prophet在R语言中进行时间序列数据预测
  6. 家里的狗为什么打不过猫
  7. easypoi 导入错误返回流_金九银十面试准备季:异常+IO与NIO流
  8. android社交软件源码,Android 社交App+java web后台整套源代码
  9. 随机抽取学生姓名html,VB中随机抽取学生姓名的程序
  10. 3dmax2015安装教程与破解方法(其中一步极为重要!!!)
  11. matlab中probIdx = 2 2,利用1stOpt1.5 pro来进行多元非线性拟合
  12. 企业级服务器固态硬盘,企业级硬盘和固态硬盘有什么区别
  13. 计算机硬盘 半导体存储器 相关概念
  14. 如何拿下最适合晚上睡不着看的网站?建议收藏!
  15. 计算机二级office应用与设计,全国计算机等级考试二级教程——MS Office高级应用与设计(2021年版)...
  16. fluent的udf在windows可以编译 linux错误,fluent中udf环境变量设置,简单可行!已试过!...
  17. 计算机中职生毕业鉴定评语,中职生学生毕业鉴定评语
  18. SpringDataRedis事务 专题------转载
  19. redux的原理、工作流程及其应用
  20. IT人的架构书单:如何赋予软件以灵魂

热门文章

  1. django-cookie的认识和基本使用与值查看
  2. BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
  3. Python库大全涵盖了Python应用的方方面面建议收藏留用!
  4. vue 项目上传到码云 解决push失败
  5. ECMAScript 6:更好的 Unicode 支持
  6. 第16届电源技术专题研讨会
  7. 4位大佬解读:“医疗人工智能、信息化、政策与科研”的新风向与新趋势
  8. LB Cluster 之一:集群及LVS基础理论详解
  9. nginx+web.py+fastcgi(spawn-fcgi)的session失效問題
  10. (转载)面试时,你会问面试官哪些问题?