JavaScript:从最受误解的编程语言演变为最流行的语言

本文探讨了JavaScript作为编程语言的历史与现状,分析了其在Web开发中的核心地位及重要性。尽管JavaScript存在诸多不足,但由于其在浏览器中的普遍适用性和独特优势,已成为最流行的编程语言之一。

这是我早期在 2008 年学习 js 时候翻译的文章,后来发邮件给作者 Douglas Crockford,他就加上我的汉化链接。如今回过头来看,翻译水平还是很差的。现在借助 AI 重新翻译了一下。

原标题:The World's Most Misunderstood Programming Language Has Become the World's Most Popular Programming Language
作者:Douglas Crockford 写于 08.3.3
原文:JavaScript: The World's Most Misunderstood Programming Language Has Become the World's Most Popular Programming Language

《计算机语言:历史与基石》一书由 Jean Sammet 写于 1969 年。内容是对 120 种编程语言的考查,对一些语言进行了详尽地介绍。Sammet 他写这本语言编年史的时候,结构化编程革命(Structured Programming Revolution)和随后流行的面向对象编程尚未到来。书籍以一张巴别塔(Tower of Babel,又译通天塔。见译注)的图片作封面。塔身由一块块砖块围成,围在里面的是各种名称的编程语言。

Jean Sammet wrote Computer Languages: History and Fundamentals in 1969. It was a survey of 120 programming languages, some in considerable detail. Sammet's book chronicled languages that were designed before the Structured Programming Revolution and the later development of Object-Orientation. The book's cover featured a picture of the Tower of Babel. Many of the bricks in the structure of the tower bore the names of programming languages.

这便是《圣经 Genesis》[11:1-9] 里面大家所熟知的故事了:

This of course refers to Genesis [11:1-9] in which some excellent engineering talent was applied to the task of building a city and a tower with which to unify the world.

耶和华降临,要看看世人们所建造的城和塔。耶和华说,看那,他们成为一样的人民,都是一样的言语,如今既作起这事来,以后他们所要做的事就没有不成就的了。所建造的城和塔。耶和华说,看呐,他们成为一样的人民,都是一样的言语,如今既作起这事来,以后他们所要做的事就没有不成就的了。 我们下去,在那里变乱他们的口音,使他们的言语彼此不通。于是,耶和华使他们从那里分散在全地上。他们就停工,不造那城了。因为耶和华在那里变乱天下人的言语,使众人分散在全地上,所以那城名叫巴别(巴别就是变乱的意思)。

But the LORD came down to see the city and the tower which the sons of men had built. And the LORD said, “Indeed the people are one and they all have one language, and this is what they begin to do; now nothing that they propose to do will be withheld from them. Come, let Us go down and there confuse their language, that they may not understand one another’s speech.” So the LORD scattered them abroad from there over the face of all the earth, and they ceased building the city. Therefore its name is called Babel, because there the LORD confused the language of all the earth; and from there the LORD scattered them abroad over the face of all the earth.

译注:西方语系中,巴别塔即《圣经》中的通天塔,大洪水之后诺亚的子孙繁衍,并试图建造通天塔直通天界,上帝不爽,于是给他们创造了不同的语言,制造了语言隔阂和交流障碍,最终通天塔也轰然倒塌。

这段意思有多种理解意思,有时理解为做人不能太傲慢;有时指的是一个寓言故事,告诉你为什么人们在说不同的语言:耶和华为防止人们达到自己的潜能而制造了 i18n 问题。不过我想把这个道理放到编程语言上去理解也是如此。编程的核心问题是复杂性的管理。如果一种语言无法帮助我们在需求变化时驾驭复杂的系统,而最终令人感到混乱,那么自然离失败不远了。

Sometimes this is read as a warning against hubris. Sometimes this is read as a just-so story explaining why people speak so many languages: The LORD created the I18N Problem to prevent humanity from reaching its potential. But I think its truest interpretation is as a metaphor for programming language design. The principle problem in programming is the management of complexity. If a language does not help us to manage very complex systems in the face of changing requirements, then confusion results, and failure follows.

为何有如此之多的编程语言?不计 Sammet 当时的数量,过去四十年已有大量新语言问世。虽说我们已经在好长的时间内是在一个高级(High Level)的环境下编程了,但真正能跨越边界,达致完备特性的语言还不是很多。那么,我们应不应该就遵循某种恰当的方式来做,来更加明显地发挥计算机的优势呢?能够以同一种绝佳的语言来统一设计,这样的话,所带来的效率毫无疑问是很明显的。因此我们也可以更有效地集中资源来配合来进行培训或开发工具。那么,为什么我们还不那样子做?

Why are there so many programming languages? Beyond the Sammet set, there have been lots of new languages and dialects developed over the past 40 years. We have been programming at a High Level for a very long time. Shouldn't we have converged on the right way to do it by now? There are obvious efficiencies that would be obtained from the design and adoption of a single, perfect language. We could focus our resources more effectively on training and tool development. Why haven't we done that?

实际上,我们已努力过,已经有好几种语言尝试过构建起大规模的、通用的设计,结果失败了。随着新语言不断加入更多的新功能,使语言本身变得越来越复杂。设计者若将语言设计得过于复杂,主说:那就让它混乱吧。

We have been trying. There have been several attempts to build massive, universal languages. They all failed. As a language design grows, increasing its applicability, it becomes more and more complex. Designers who makes their language too complex will confuse the language, sayeth the LORD.

因此我们看到取而代之的是许多的专用语言。在完成一些任务的领域中,专用语言可以非常高效地解决,而且本身是不错的。对于要完成某些任务的人而言,选择好的语言,自然如虎添翼。

So we see instead a large number of more specialized languages. A specialized language will be very effective for some range of tasks. And that's good enough. For people who have to accomplish those tasks, having the the right language can be, well, a godsend.

一门编程语言,本质上是将某种计算模型与表达性语法拼接而成。大多数语言都包含一些基本数据类型,如数字和文本(奇怪的是,多数语言称之为“字符串”),以及用于修改和组合数据的操作,某种形式的条件分支与循环,还有把一些常用的操作封装为更便捷的命令中。变化行如幻影,线路莫测,就像一道菜和一首乐曲每一次都不尽相同。语言设计的艺术在于清楚哪些是舍的(leave out)。一门良好语言,它里面的功能应该是和谐地工作在一起,帮助我们更好地明白问题和找到其解决方案的最优表达形式。

A programming language cobbles a model of computation with some sort of expressive syntax. Most languages will have a set of useful values such as numbers and texts (which most languages, strangely, call strings), and operations that mutate and synthesize values and some form of variation or repetition, and some way of packaging operations into more convenient idioms. The variations that are possible are endless, much like the variations possible in the preparation of a fine meal or a work of music. The art in language design is knowing what to leave out. The features of a good language work together harmoniously. A good language helps us to better understand a problem and to find the best expression of its solution.

一门好的语言由几组功能所构成,但哪一项的功能才是最好的就永远没有结论。程序员可以不断地讨论这个话题和是否比其他的语言优秀。这里不是说功能不重要。功能它非常重要。只不过我们当时还没清楚它真的那么重要。

A good language is composed of a limited set of features. But there is little agreement on which features are best. Programmers can argue endlessly about features and whether they make one language better than another. This does not mean that the features don't matter. They matter greatly. We just don't understand yet how they matter.

于时髦与技术之间而言,语言设计需要做更多的倾向于时髦方面。时尚是极客气息最浓的艺术中的重要因素,看起来可能怪怪的,却一点都不假,事实如此。无论语言的设计有多么精巧,若以另一种语法出现的话,则是另类,没有希望得到广泛使用的。这样会约束了语言的进化。

Language design has more to do with fashion than technology. It may seem strange that fashion is a major factor in the nerdliest of arts, but it is true. A language with radically different syntax, for example, has no hope of finding broad adoption, regardless of the brilliance of its design. This tends to constrain the rate at which languages evolve.

像音乐和食物一样,编程语言也属于时间的产物。语言设计的深层问题不是技术上的,而是属于心理上的问题。一门编程语言应该与我们认知的结构相吻合,从而帮助我们更高效率地判断问题。

Like music and food, a programming language can be a product of its time. The deep problem in language design is not technological, it is psychological. A programming language should equip us with structures that help us to reason more effectively.

编程语言就像猫。与其修理一只老猫,不如换一只新猫来得容易(比喻:重造语言比改造旧语言更容易)。照此理,大多数语言成功后由后来的升级版所取缔。然而重新规划的语言很难到达旧版所达到的那种成就。例如,Fortan 曾是语言翘楚,好些年亦不断改进,不过 Fortran IV 的声誉始终非现代化后的 Fortran 方言所能企及。类似地,Pascal 是结构化编程的流行语言,但其 OO 的版本却难盖原 Pascal 之光芒。结果,它渐渐被取代。

Programming languages are like cats. It is easier to get a new cat than to get an old cat fixed. Most successful languages are ultimately replaced by upstarts. Remodeled languages rarely match the glory of the original. Fortran was once the king of languages. It has been revised several times over the years, but the modernized dialects experienced a fraction of the prestige of Fortran IV. Similarly, Pascal was a popular structured programming language, but none of the object oriented dialects ever approached Pascal's glory. Instead, languages tend to be superseded.

专业用途的语言做起来挺有意思的,这也解释了它们的数量为什么是如此地多。如果一门语言结构不太庞大,那么一个程序员就可以掌控它了。大多数语言可以由一个设计师自己创造出来。Pascal:Wirth。C: Ritchie。C++: Stroustrup、Java: Gosling. C#:Hejlsberg、Rebol: Sassenrath、Python:van Rossum、PHP: Lerdorf、Perl:Wall、Ruby: Matsumoto、Lua: Ierusalimschy. E:Miller、JavaScript:Eich。

Specialized languages are fun to make, which partly explains why there are so many. If a language doesn't get too big, it can be managed by a single designer. Most languages can be identified with a single designer. Pascal: Wirth. C: Ritchie. C++: Stroustrup. Java: Gosling. C#: Hejlsberg. Rebol: Sassenrath. Python: van Rossum. PHP: Lerdorf. Perl: Wall. Ruby: Matsumoto. Lua: Ierusalimschy. E: Miller. JavaScript: Eich.

大多数语言失败之处在于不够清晰。有的语言只是在单个项目或某个公司中使用而已,很少能够脱颖而出成为重要的语言。

Most languages die in obscurity. Only a few are able to build a following beyond a single project or company. And only a very small number of languages become important.

一种编程语言变得重要,通常有两种途径。第一种是作为重要思想的发源地或试验场,例如 Smalltalk 和 Scheme。这些语言虽然使用不广,如今也显得过时,但普遍被认为极具开创性,对后来的语言设计产生了深远影响。

There are two ways that a language becomes important. The first is as a source or proving ground for important ideas. This includes languages like Smalltalk and Scheme. These languages are not widely used, but are generally recognized as brilliant, but out of fashion. They have a powerful influence on language designers.

第二种方式,则是通过广泛的流行而变得重要。

The second way that a language becomes important is by becoming popular.

程序员在选择编程语言时需考虑诸多因素,但若目标是为 Web 浏览器开发,目前却别无选择——只能使用 JavaScript。

There are many things that a programmer must consider when selecting a program language unless that programmer is writing for web browsers where the only choice currently is JavaScript.

JavaScript 是一种令人震惊的语言,而且是在最糟糕的意义上。这种令人震惊的特质让它背负了极差的名声。JavaScript 还与 DOM(文档对象模型)紧密绑定,而 DOM 本身就是一个糟糕透顶的 API。人们常常分不清哪些问题是 JavaScript 本身的,哪些是 DOM 带来的。(译注:尤其在跨浏览器兼容性方面,问题尤为突出。)。虽然很多人呼吁改进 JavaScript,但仅靠语言本身的改良,对改善 Web 开发者的处境帮助有限。这门语言背负了太多特性,其中许多相互冲突或设计拙劣。正如同皇帝约瑟夫对莫扎特所说的:“音符太多了。”

JavaScript is an astonishing language, in the very worst sense. Its astonishing nature led to a very bad reputation. JavaScript is also coupled with The DOM, a horrendous API. There is a lot of confusion about where JavaScript ends and the DOM begins. There is lots of chatter about improving JavaScript, but that by itself would do little to improve the lives of web developers. The language is burdened with too many features, including many that interact badly or were poorly designed. It is a language that has, as Emperor Joseph would say, too many notes.

那么,这样一门缺陷明显的语言,为何会成为 Web 唯一的编程语言?回到萌芽时期,Brendan Eich 说服了 Netscape 的管理层,认为 Navigator 浏览器必须拥有自己的脚本语言,而且必须是全新的语言,不能采用现成的。于是,一门新语言在极短时间内被设计并实现出来。话说回来,还真得要设计新的语言,那时并没有替代品。微软的 Internet Explorer 团队为了抢占 Netscape 的市场份额,仔细地逆向工程了其脚本语言,连那些明显的缺陷也一并复制。其他浏览器厂商纷纷效仿微软的做法。实际上,并没有任何标准规定浏览器必须实现 JavaScript——但它却是唯一被所有主流浏览器支持的语言。除了 JavaScript 外,没有其他语言能够在所有浏览器行得通。再者彼时互联网发展充满着一切未知,也不存在对某个语言进行仔细地调研,没有什么前期性的调研或周密的设计。它只是出自 Netscape 之手然后不断被复制而已。若我们人为地、刻意地、费煞苦心地创造一个类似 JavaScript 的东东但又为它过早地去建立标准,那样的东西,说不定不是我们想要的。

So how did a language with such obvious deficiencies become the Sole Programming Language of the Web? Brendan Eich convinced his pointy-haired boss at Netscape that the Navigator browser should have its own scripting language, and that only a new language would do, a new language designed and implemented in big hurry, and that no existing language should be considered for that role. The Internet Explorer team at Microsoft wanted to capture Netscape's market share, so they carefully reverse engineered Netscape's language, huge hairy warts and all. The other browser makers followed Microsoft's lead. There is no standard that says that a web browser must implement JavaScript; JavaScript is the only language implemented in all popular web browsers. There was no careful review of the language or its problem domain. There was no review of its suitability or the soundness of its design. It was slapped together at Netscape, and then copied elsewhere. Given the process that created JavaScript and made it a de facto standard, we deserve something far worse.

JavaScript 纵有千般不是,其内部却做对了一件核心的事情。而当你一步一步慢慢到内部了解其奥秘,便会发现 JavaScript 是一个富有表现力且强大的编程语言。许多 Ajax 库现在用 JavaScript 来处理 DOM,以网页的形式来生成应用程序的交互平台。Ajax 变得更为流行正因 JavaScirpt 还算可以的,而且某些方面还超乎我们的想象。

But despite JavaScript's astonishing shortcomings, deep down, in its core, it got something very right. When you peel away the cruft, there is an expressive and powerful programming language there. That language is being used well in many Ajax libraries to manage and augment the DOM, producing an application platform for interactive applications delivered as web pages. Ajax has become popular because JavaScript works. It works surprisingly well.

JavaScript 曾是世界上最被误解的编程语言。它明显的缺陷、不合潮流的编程模型、发布时的刻意误导,以及荒谬的名字,使其被大多数资深程序员视为不值一提。但 Ajax 技术的兴起,给了 JavaScript 一次重生的机会。

JavaScript was the world's most misunderstood programming language. Its obvious defects, its unfashionable programming model, intentional mispositioning at its introduction, and its ridiculous name caused it to be rejected as unworthy by most knowledgeable programmers. But Ajax gave JavaScript a second chance.

正因为 JavaScript 是浏览器的专属语言,Web 浏览器又已成为主流的应用交付平台,而 JavaScript 本身也并非一无是处,它最终成为了世界上最流行的编程语言。它的影响力持续扩大,如今已被嵌入各种应用与环境中,变得愈发重要。

Because JavaScript is the language of the web browser, and because the web browser has become the dominant application delivery system, and because JavaScript isn't too bad, JavaScript has become the World's Most Popular Programming Language. Its popularity is growing. It is now being embedded in other applications and contexts. JavaScript has become important.

有时候,运气比聪明更重要。

It is better to be lucky than smart.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sp42a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值