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. CF912D Fishes 期望
  2. 小白科普:虚拟化简史
  3. Oracle 获取字符的位置并截取字符串
  4. 1:tcl-8.5.13-8.el7.x86_64: [Errno 256] No more mirrors to try.
  5. 客户端相关知识学习(二)之h5与原生app交互的原理
  6. 一致性哈希算法 应用
  7. Docker 是什么,组成
  8. 最近很火的人生重开模拟器HTML网页源码
  9. php 远程连接 sqlserver,Linux下PHP远程连接SqlServer数据库
  10. 9.RabbitMQ实战 --- 使用REST API控制Rabbit
  11. java调用python脚本并向python脚本传递参数
  12. 【元胞自动机】基于元胞自动机实现艺术图像处理附matlab代码
  13. 什么是物联网技术
  14. Android Studio 全局搜索快捷键
  15. 物流中的独立节点路由试算
  16. angular的ngStrictDi
  17. 计算机打印状态错误,打印机开始打印就显示错误怎么办?
  18. 看尚c49s电视恢复出厂设置后无法注册问题
  19. norflash/nandflash 启动分析
  20. 计算机系统ARM64拆除炸弹

热门文章

  1. redis报错解决,MISCONF Redis is configured to save RDB snapshots
  2. jquery - 正则表达式
  3. python-运算符之算术运算符
  4. 安装PostgreSQL单机版
  5. 7月31日云栖精选夜读 | 在浏览器上也能训练神经网络?TensorFlow.js带你玩游戏~...
  6. 史上最快! 10小时大数据入门(一)-大数据概述
  7. 同步与阻塞的区别与联系
  8. DevExpress v15.1:XAF控件升级
  9. 浓浓的亲情 2008-10-6 8:53:00 (21ic)
  10. 理解复杂的C/C++声明 const, typedef , 函数指针(转贴)