引入 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.pswp.cn/news/396100.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396100.shtml
英文地址,請注明出處:http://en.pswp.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.…