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

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值