引入 javascript_在您JavaScript项目中引入类型安全性? 再想一想

引入 javascript

by James Wright

詹姆斯·赖特(James Wright)

在您JavaScript项目中引入类型安全性? 再想一想 (Introducing Type Safety To Your JavaScript Project? Think Again)

更新— 2017年2月1日 (Update — 1st February 2017)

I’ve heard various counter-arguments regarding type safety in JavaScript since I first published this article, and while I still believe a lot of projects do not require the use of a typed JavaScript superset, I admit that I was too hasty in publishing this article. Some appropriate use cases have subsequently grabbed my attention:

自从我第一次发布本文以来,我就听到过关于JavaScript中类型安全的各种反争论,尽管我仍然相信许多项目不需要使用类型化JavaScript超集,但我承认我在发布此文章时过于仓促文章。 一些合适的用例随后引起了我的注意:

  • Glimmer, the low-level rendering engine behind Ember, is written in TypeScript to promote monomorphic call sites, aiding performance when executed by V8 and potentially other JavaScript engines

    Ember背后的低级渲染引擎Glimmer是用TypeScript编写的,以促进单态调用站点 ,在由V8和可能的其他JavaScript引擎执行时有助于提高性能

  • Visual Studio Code benefits from TypeScript due to the sheer size of the project; given that it’s distributed as a desktop application, having one codebase rather than reconciling individual packages at build time is, in my opinion, a sensible option

    由于项目庞大, Visual Studio Code受益于TypeScript。 鉴于它作为桌面应用程序分发,因此在我看来,拥有一个代码库而不是在构建时协调各个软件包是一个明智的选择

  • Sect (admittedly a project of my own, so there’s potential bias here!) is written in TypeScript so that consumers can write large, modular games for the web while reliably reducing runtime errors resulting from misspellings and other issues that arise as a result of JavaScript’s dynamic nature

    Sect (当然是我自己的一个项目,所以这里可能有偏见!)是用TypeScript编写的,以便消费者可以为Web编写大型的模块化游戏,同时可靠地减少由于拼写错误和其他JavaScript问题引起的运行时错误。动态性质

I have additionally realised that writing smaller libraries in TypeScript and publishing them with the type definitions generated at build time simultaneously enables their seamless integration with typed and traditional JavaScript projects, thus giving developers a wider technological choice.

我还意识到,使用TypeScript编写较小的库并使用在构建时生成的类型定义同时发布它们,可以使它们与类型化传统JavaScript项目无缝集成,从而为开发人员提供了更广泛的技术选择。

Nonetheless, for posterity’s sake, here’s the original article in its entirety.

尽管如此,为了后代的缘故,这是本文的全文。

Today, I encountered an article regarding the launch of JS++, which claims to “fix JavaScript’s lack of type safety.” Funnily enough, we already have TypeScript, ST-JS, and Scala.js, which assist developers in ultimately achieving the same goal.

今天,我遇到了一篇有关JS ++推出的文章,该文章声称“修复JavaScript缺乏类型安全性”。 有趣的是,我们已经有了TypeScript , ST-JS和Scala.js ,它们可以帮助开发人员最终实现相同的目标。

Before I launch into this tirade, allow me to highlight three important points:

在着手介绍这一长篇文章之前,请允许我重点强调以下三点:

  • I’ve previously written a tutorial on establishing a simple TypeScript project. I see the hypocrisy but my opinions have changed since I published it over a year ago

    之前,我已经编写了有关建立简单TypeScript项目的教程 。 我看到了伪善,但是自从一年多以前发表以来,我的观点已经改变

  • Strong typing and static typing are vital paradigms. The former provides transparency over the entities represented in one’s code, their relationships, and the functionality they may be expected to provide, while the latter is an important, compile-time safety net in complex systems. I come from a C# background, so I have first-hand experience of this

    强类型和静态类型是至关重要的范例。 前者提供了代码中所代表的实体,它们之间的关系以及所期望提供的功能的透明性,而后者则是复杂系统中重要的编译时安全网。 我来自C#背景,所以我对此有第一手经验
  • I also love JavaScript, given its inherent flaws, many of which have been addressed with ECMAScript 6 and 7

    考虑到JavaScript固有的缺陷,我也很喜欢JavaScript,其中许多缺陷已通过ECMAScript 6和7进行了解决。

So why am I generally against static typing in JavaScript?

那么,为什么我通常反对JavaScript中的静态类型?

Predominantly, what makes JavaScript such a powerful language is its weakly-typed nature; it’s trivial to implement branches of logic via type coercion, and it’s so easy to create object instances of an arbitrary type. Furthermore, the lack of compilation (unless one is using a transpiler or build tool such as Babel, for example) makes development incredibly fast, as long the code doesn’t result in any bizarre behaviours. In my opinion, this is what makes it so powerful for frontend and simple backend (e.g. IoT) development.

最主要的是,使JavaScript如此强大的语言是其弱类型的本质。 通过类型强制实现逻辑分支很简单,而且创建任意类型的对象实例非常容易。 此外,缺乏编译功能(除非使用诸如Babel这样的编译器或编译工具),只要代码不会导致任何怪异的行为,开发速度就非常快。 在我看来,这就是使其对于前端和简单后端(例如IoT)开发如此强大的原因。

I personally believe that if one is developing a system so complex that it requires type safety, then one should be using a language that supports it at its core; writing a guidance system, which involves complex mathematical operations, in JavaScript is insane.

我个人认为,如果开发的系统如此复杂以至于需要类型安全,那么应该使用一种支持其核心的语言。 用JavaScript编写包含复杂数学运算的指导系统简直是疯狂。

My main concern with these JavaScript tools and supersets is that they compile to, well, JavaScript; these programs are consequently running in a dynamic context, thus the same side effects could still occur. TypeScript, for example, may be statically typed (i.e. type information is gathered and analysed at compile-time), but one must have full confidence that the resulting code will still run as expected. Yes, of course even statically-typed languages are usually compiled to a lower-level language, which is then typically interpreted, but these target languages were surely designed with typing as a first-class citizen; as an example, Microsoft’s JIT compiler for .NET still implements runtime type-checking of its intermediate language before compiling to native code.

我对这些JavaScript工具和超集的主要担心是,它们可以编译为JavaScript。 因此,这些程序在动态环境中运行,因此仍然可能发生相同的副作用。 例如,TypeScript可以是静态类型的(即在编译时收集并分析类型信息),但是必须完全相信生成的代码仍将按预期运行。 是的,当然,即使是静态类型的语言也通常会编译成较低级别的语言,然后通常对其进行解释,但是这些目标语言肯定是通过打字作为一等公民而设计的; 例如,Microsoft的.NET JIT编译器在编译为本机代码之前仍对中间语言执行运行时类型检查 。

Furthermore, when undertaking frontend development, I’m still of the mindset that JavaScript should be used to complement HTML and CSS solutions e.g. adding classes to elements, making HTTP calls to backend services etc. While the web has matured in terms of frameworks for authoring larger, UI-based applications (FYI, I have written larger apps with React.js and vanilla JS too; I love both), I prefer to keep my JS as light as possible. I understand that this isn’t always a possibility in reality, but if backend systems serve as the source truth for fundamental business logic, then frontend code becomes lighter and less redundant; in this respect, what benefits will a type system bring?

此外,在进行前端开发时,我仍然保持思维定势,应该使用JavaScript来补充 HTML和CSS解决方案,例如向元素添加类,对后端服务进行HTTP调用等。虽然Web在创作框架方面已经成熟较大的,基于UI的应用程序(仅供参考,我也使用React.js和Vanilla JS编写了较大的应用程序;我俩都爱),我更喜欢保持JS的轻量化。 我了解实际上这并不总是可能的,但是如果后端系统充当基本业务逻辑的源头,那么前端代码将变得更轻便,冗余度也将减少。 在这方面,类型系统会带来什么好处?

Following my point of the size of frontend software, my current work entails writing concentrated web applications for each concern of the overarching system; as opposed to one large single-page application for our shop, which contains a product list view, a product details view, and a purchase journey view, we have respective Node.js-backed apps for them. Evidently, this is a best practice in terms of loose coupling and resilience, but from a code point of view, it allows one of focus more easily on the implementation of one area of our frontend.

按照前端软件的大小,我目前的工作是针对总体系统的每个方面编写集中的Web应用程序。 与我们商店的一个大型单页应用程序相反,该应用程序包含产品列表视图,产品详细信息视图和购买过程视图,我们为它们提供了各自的Node.js支持的应用程序。 显然,就松散耦合和弹性而言,这是最佳实践,但是从代码角度来看,它可以更轻松地将重点放在前端某个区域的实现上。

My final argument is this; is JavaScript really that difficult to learn? As I’ve said before, ECMAScript 5 itself is a flawed language; the different function invocation patterns and how they affect the `this` keyword and lack of block scoping, for example, can make it difficult for beginners. However, with ECMAScript 6, plus the plethora of amazing resources out there, it’s easy to overcome and be aware of these issues. Why not just skip the middle man and learn the language directly?

我的最后一个论据是: JavaScript真的很难学习吗? 如前所述,ECMAScript 5本身是一种有缺陷的语言。 例如,不同的函数调用模式以及它们如何影响`this`关键字以及缺少块作用域,可能会使初学者感到困难。 但是,有了ECMAScript 6,再加上大量的惊人资源,就很容易克服并意识到这些问题。 为什么不跳过中间人直接学习语言呢?

I’ll close by saying I’m a fan of all typing approaches, but some suit certain scenarios more than others. If JavaScript works best for the majority of frontend software, given its ubiquity within development teams and their projects, then surely it doesn’t need a superset. Additionally, there is a truckload of languages that are inherently type safe, so stop reinventing the wheel!

最后我会说我是所有打字方法的爱好者,但是有些方法比其他方法更适合某些情况。 如果JavaScript在开发团队及其项目中无处不在,那么它对于大多数前端软件来说都是最佳的,那么肯定不需要超集。 另外,还有一大堆本来就是类型安全的语言,所以别再发明轮子了!

翻译自: https://www.freecodecamp.org/news/stop-bringing-strong-typing-to-javascript-4da0666cba6e/

引入 javascript

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/396100.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

劣币驱逐良币通俗解释_如何通俗理解劣币驱逐良币的问题,同时如何扭转这个现象?...

以牛奶为例谈劣币驱逐良币。【原来】大家都卖的纯正的纯牛奶,因为。牛奶的来源和生产工艺基本上都差不多,所以说产品的品质也没有什么大的差异。【后来】奶制品企业为了争夺市场开始打价格战,你卖5块我就卖4块,你卖4块我就卖3块5&…

c语言里 如何取得线程的lpparameter'参数,请问线程函数如何访问对话框类中的变量...

我线程函数定义在对话框类的实现文件中需要访问对话框类头文件中声明的变量怎么才可以?i_noname(晚九朝五) 于 2005-9-15 16:14:25DWORD WINAPI Proc(LPVOID lpParameter ){CDialog *pDlg (CDialog *)lpParameter;...}启用线程时把对话框指针传进去AfxBeginThread(…

Android内存优化——内存泄露检测分析方法

上一篇文章总结了一些常见的内存泄露场景及优化方案,这篇文章继续总结内存泄露的一些常用的检测和分析方法。 Lint代码检查 AndroidStudio自动Lint代码检查工具,一些常见的代码警告Lint工具都会给我们提示。使用也比较简单: Analyze —> I…

ImageLoader加载图片

先导universal-image-loader-1.9.3包 在application配置 android:name".MyApplication" intent权限 1 package com.ch.day13_imageloaderdemo;2 3 import java.io.File;4 5 import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache; 6 impor…

hackintosh黑苹果_为什么您的下一个Mac应该是Hackintosh?

hackintosh黑苹果by Sebastian Dobrincu塞巴斯蒂安多布林库(Sebastian Dobrincu) 为什么您的下一个Mac应该是Hackintosh? (Why Your Next Mac Should Be a Hackintosh ?) I just finished a 48-hour Hackintosh building marathon. It was a fun experience and I…

一张图解释什么是遗传算法_遗传算法简介及代码详解

(allele)数据值,属性,值基因座(locus)位置,iterator位置表现型(phenotype)参数集,解码结构,候选解染色体:又可以叫做基因型个体(individuals)群体/种群(population):一定数量的个体组成&#xf…

c语言程序设计学生程序查询,《c语言程序设计报告-学生信息管理系统》.doc

《c语言程序设计报告-学生信息管理系统》中南大学??C语言程序设计实践报告?????题 目 学生信息管理系统学生姓名 张眼指导教师 刘伟荣学 院 信息科学与工程学院专业班级 电气信息1113班完成时间 2012年6月28日星期四?1.设计任务及设计目标学生信息管理系统是基于系统主…

ListString 和 ArrayListString的区别

最近对这两个问题比较懵逼&#xff0c;关于List和ArrayList、List<String> list new ArrayList<String>(); 好了&#xff0c;先搞明白List 和 ArrayList吧。 List是一个接口&#xff0c;是Collection接口的一个子接口&#xff0c;是一个有序的集合。 ArrayList是L…

java城市级联一次查询_我的城市没有任何设计活动,所以我自己组织了一次。...

java城市级联一次查询by Marty Laurita由Marty Laurita 我的城市没有任何设计活动&#xff0c;所以我自己组织了一次。 (There weren’t any design events in my city, so I organized one myself.) “The meeting of two personalities is like the contact of two chemical…

Access denied for user 'root'@'localhost' (using password: YES) 问题解决小记

初学php&#xff0c;按照视频安装后好mysql后 终端运行命令 mysql -u root -p 然后输入安装mysql时输入的密码六个1&#xff0c;会报这样的错误&#xff1a;Access denied for user rootlocalhost (using password: YES) &#xff1b; 百度了一大堆&#xff0c;大海捞针一般找…

汇编总结2

一、寄存器 一个典型的cpu是由运算器&#xff0c;控制器&#xff0c;寄存器等器件组成的。 内部总线实现CPU内部各个器件之间的联系 外部总线实现cpu和主板上其他器件的联系 AX,BX,CX,DX通用寄存器 SI,DI,BP,SP基址和变址寄存器 CS,SS,DS,ES段寄存器 IP,FLAGS指令指针和标志寄存…

创业者具备的五大技能_一、如今大学生创业需要具备哪些知识与技能?

我参加过两次互联网&#xff0b;大赛&#xff0c;分别获得过省赛的金奖与银奖&#xff0c;还曾参加山东省大大小小比赛26场&#xff0c;金奖累计获得12次。对当代创业的大学生所应具备哪些基本素质深有体会。&#xff08;1&#xff09;技能&#xff1a;1、自我认知及科学规划能…

c语言定时器回调函数的参数,定时器的简单实现即回调函数的运用

&#xfeff;&#xfeff;这两天在 研究回调函数就想实现简单的定时器&#xff0c;如下是鄙人的程序望指教。ios#include #include using namespace std;app#define MAXNUM 256函数typedef void (*timerProcessFunc)(void*);spatypedef struct{unsigned int id;int timeout; /…

BZOJ3387栅栏行动

首先&#xff0c;很容易想到Dp。设f[i][0]表示第i个栅栏走左边的最短路&#xff0c;f[i][1]表示第i个栅栏走右边的最短路。 所以&#xff0c;我们要找一个刚好在第i个栅栏的左右边界下面的栅栏。如图所示&#xff1a; 则有&#xff1a; f[i][0] min(f[k][0] |Left[i] - Left[…

udacity开源的数据_评论:Udacity数据分析师纳米学位计划

udacity开源的数据by David Venturi大卫文图里(David Venturi) 评论&#xff1a;Udacity数据分析师纳米学位计划 (Review: Udacity Data Analyst Nanodegree Program) Udacity’s Data Analyst Nanodegree program was one of the first online data science programs in the …

凌晨四点钟深圳的风景

科比有过一句很励志的故事&#xff1a;凌晨四点钟洛杉矶的风景。 很多人把科比当成榜样&#xff0c;不仅仅因为他精湛的球技&#xff0c;更是因为他远超常人的职业精神。 其实做到这一点&#xff0c;并不难&#xff0c;难的是坚持。坚持那么早时间起床&#xff0c;坚持十年如一…

小程序沉浸式_古北水镇红叶祭嵌入戏精学院 全新文旅沉浸模式让游客嗨起来...

2020年10月17日-24日&#xff0c;古北水镇第二届红叶祭火热来袭。今年除了“超级漫展二次元度假”的模式&#xff0c;古北水镇与顶级沉浸互动体验运营方——INX戏精学院合作&#xff0c;在深度体验空间的同时&#xff0c;加入了互动式的实景游戏体验&#xff0c;通过演员互动&a…

又拍云刘平阳,理性竞争下的技术品牌提升之道

云服务市场趋渐平稳&#xff0c;在这种情况下&#xff0c;就需要通过对某一项技术的深入应用来实现服务的精致化。同时&#xff0c;对品牌的打造和包装也必不可少。\\又拍云在2010年开始提供云服务&#xff0c;经过多年的发展&#xff0c;以及市场策略的转变&#xff0c;决定对…

编写代码的工作在哪找_编写事件代码如何帮助我获得了出色的工作

编写代码的工作在哪找Everyone kept telling me about the importance of networking, but it was always something I blew off. I’m pretty quiet and introverted, particularly when meeting strangers. I thought I just wasn’t built for networking.每个人都在不断告诉…

int x = 0x13 c语言,2004年7月全国高等教育自学考试微型计算机原理与接口技术试题...

课程代码&#xff1a;02205第一部分 C语言程序设计一、单项选择题(在每小题的四个备选答案中&#xff0c;选出一个正确答案&#xff0c;并将正确答案的序号填在题干的括号内。每小题2分&#xff0c;共10分)1.4位无符号二进制数表示的数的范围是( )。A.0&#xff5e;9999 B.…