博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为什么要在JavaScript中使用静态类型? 我们是否应该使用它们?
阅读量:2530 次
发布时间:2019-05-11

本文共 3283 字,大约阅读时间需要 10 分钟。

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 and if you haven’t already!

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

那么我们是否应该在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.

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

翻译自:

转载地址:http://vhewd.baihongyu.com/

你可能感兴趣的文章
挑战程序设计竞赛 P131 区间DP
查看>>
【例9.9】最长公共子序列
查看>>
NSFileManager打印目录下的文件的函数
查看>>
Selenium自动化-调用Mysql数据库
查看>>
项目一
查看>>
[转载]AAF灵便应用框架简介系列(6):休息一下,泛谈面向对象 Why OO+多层结构?...
查看>>
android EditView ime
查看>>
javascript 学习随笔7
查看>>
<P>标签小细节
查看>>
Linux 命令 - netstat
查看>>
mac 关闭&&显示隐藏文件命令
查看>>
JavaScript 循环绑定之变量污染
查看>>
poj 1038 Bugs Integrated, Inc. 三进制状态压缩 DFS 滚动数组
查看>>
zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
查看>>
Wordpress解析系列之PHP编写hook钩子原理简单实例
查看>>
怎样看待个体经济
查看>>
不明觉厉的数据结构题2
查看>>
面向对象编程思想概览(四)多线程
查看>>
二十三种设计模式及其python实现
查看>>
Math类、Random类、System类、BigInteger类、BigDecimal类、Date类、SimpleDateFormat、Calendar类...
查看>>