下面这篇开场白是译自<>一书, 恰好作为自己对JavaScript
及相关的语言, 技术标准的补充.
JavaScript is a unique language梔espite its name, which has caused a great deal of confusion, it is not directly related to Java. When JavaScript was first introduced, Java was dominant, and it must have seemed like a good idea to tie JavaScript to Java.
尽管它的名字引起了很大程度上的混乱, JavaScript却是一门独立于Java的语言,
它跟Java没有什么直接的关系. JavaScript被催生出来时, Java的确是居于统治地
位, 很可能人们认为让它跟Java沾点关系是个不错的主意.
Things have shifted dramatically, however; JavaScript has become far more popular than Java for web use. Whereas Java has gone on to become a relatively complex, involved language, JavaScript has remained remarkably the same over the years梐 lightweight, fast, and powerful language that is, quite simply, the programmer's favorite梚t's by far the favorite language of the web.
然而事情很快发生了戏剧性的变化, JavaScript在Web应用上变得远远比Java更为
流行. 当Java超着相对来说错综复杂的方向发展时, JavaScript却仍然在这些年里
保持着一个轻量级的, 快速和强大的语言, 它可能是web上最为程序员喜爱的一种
简单语言.
It all began in Netscape Communications Corporation in 1995, when a developer named Brendan Eich, who they had just lured away from MicroUnity Systems Engineering, was given a new project to work on. The original idea was to make programming more accessible than it was becoming in Java, and to somehow make the Netscape Navigator's new Java support more open to non-Java programmers. Eich took a look around and decided that what was needed was a scripting language that was fast and easy to use. Unlike Java, which first has to be compiled into "bytecodes," JavaScript was to be embedded right in the web page itself, and downloaded with that page. And as Java itself has become more and more inaccessible, JavaScript's star just continues to rise.
所有的故事都起源于1995年的Netscape公司, 当时一个叫Brendan Eich的开发人员
刚被从MicroUnity 系统工程引诱过去, 给了他一个新的项目. 最初的主意是让它
象Java一样更容易编程, 某种程度上也是让Netscape浏览器新的Java支持对于那些
非Java程序员也显示更加开放. Eich环伺四周然后发现需要的是一种快速易用的脚
本语言, 不象Java那样必需被编译成字节码, JavaScript将被嵌在Web页面内部,
随页面一起下载. 当Java本身变得越来越不那么容易接触, JavaScript作为耀眼的
新星却正冉冉升起.
Brendan Eich called his creation LiveScript, but it was renamed JavaScript. (In fact, many people consider the name change a big mistake.) Although developed by Netscape, Sun had the trademarks on Java, and the name JavaScript is actually a trademark of Sun Microsystems, Inc. The new language was announced in a Netscape and Sun joint press conference on December 4, 1995. They originally positioned it as a "complement" to Java and HTML, rather than a simple support language for Java.
Brendan Eich把他的新发明叫作LiveScript, 但后来被改成了JavaScript. (事实
上, 很多人认为这个名字是一个很大的失误) 虽然诞生地是Netscape, 却是Sun公
司同时拥有java和JavaScript两个语言的商标. 新的语言在1995年12月4由
Netscapet和Sun举办的联合发布会上被宣布. 他们最初把它定位为对Java和HTML的
一个补充, 而不是对Java提供一种简单支持的语言.
However, JavaScript rapidly slipped out of Java's shadow. Programmers took to it at once, and although there were problems with bugs and later with security, JavaScript was a success. It became clear that, as Eich had realized, programming a web page in the browser made all kinds of things possible that couldn't be done if you needed to reload the page from the server. (In fact, the most popular, original use for JavaScript was to swap images when the mouse rolled over them, and that continues to be a very popular thing to do.)
然而, JavaScript很快就走出了Java的阴影. 程序员们很快地接受了它, 虽然里面
有一些bugs而且后来又有一些安全性问题, JavaScript仍然获得了成功. 就象
Eich意识到的那样, 对页面编程使得你必需从服务器重载页面时所有过去没办法的
事成为可能. (事实上, JavaScript的最初也是最炫的应用就是为了在鼠标滑过时
交换显示图片, 现在这也仍然是很多人喜欢的用法).
.
Web programmers found JavaScript programming fun. To some extent, that meant that many programmers didn't take it seriously. Java was a serious language, and as time has gone on, it has become much more serious梥erious, unfortunately, to the point of inaccessibility for much of its intended audience. JavaScript, originally a guilty pleasure for many, started to be accepted by thousands of programmers. As browsers became more powerful and could run scripts faster (eliminating screen flicker and other problems), programmers discovered that JavaScript was indeed a viable development tool. And all it took was a few lines of code to get things working, making buttons work, and setting cookies.
Web程序员发现JavaScript编程很有趣. 某种程度上这也说明很多程序员并不认识
它是一种严肃的语言. Java是一门严肃的语言, 随着时间的发展, 它还会变得更加
严肃, 让它不幸的受众也更加难以接近它. JavaScript这个最初只是人们一种罪恶
的消遣的语言, 却被成千上万的程序员所接受. 当浏览器变得更强大并能以更快的
速度运行脚本时(排除屏幕闪烁和其它问题), 程序员发现JavaScript的确是一种可
行的开发工具. 所有要做的事只是几行代码, 按钮运转起来了, cookies也得以设
置, 一切都活起来了.
As JavaScript started making waves, Netscape's chief competitor, Microsoft, could no longer ignore it. It's interesting to speculate how things would have gone if JavaScript had been a public standard in those days, developed, say, by a neutral third party. But JavaScript was a product of Microsoft's direct competitor in this area, Netscape, so Microsoft decided to create its own version of JavaScript, which it called JScript.
当JavaScript热掀起时, Netscape的主要竞争对手微软再也不能无视它的存在了.
想一想那时候如果JavaScript是被一个第三方的中立方开发的公共标准的话情况又
该如何也是很有趣的. 但是JavaScript却是微软的直接竞争对手的产品. 所以微软
决定创造自己版本的JavaScript, 叫作JScript的东东.
The first official version of JScript was released on July 16, 1996, in Internet Explorer 3.0. From then on, there was both JavaScript and JScript, and the resulting split personality for the language between Netscape and Microsoft has had repercussions that echo down to today. The original JScript was much like a reduced version of JavaScript 1.1, however, and the differences confused programmers. At that point, Microsoft had created its own scripting language, VBScript, based on its popular Visual Basic language, and was hoping to successfully rival JavaScript. JavaScript was triumphant, however, and VBScript is a very distant second these days.
JScript的第一个官方版于1996年7月16日随IE3.0发布, 从那时起就有了两个东西:
JavaScript和JScript, 由此造成的Netscape和微软之间两种语言的分立直到今天
还造成回响. 最初的JScript很象是JavaScript1.1的消减版, 而且两者的不同让程
序员们尤其感到迷惑. 那时, 微软也创造了自己的脚本语言, VBScript, 基于它那
流行的语言VB, 微软希望它能成为JavaScript的有力对手. 然而JavaScript仍然稳
操胜券, VBScript落在了第二的位置.
To bring their JScript support up to full JavaScript 1.1, Microsoft later released a second version of Internet Explorer 3.0. So started the cross-browser and cross-browser version problems that have made life harder for the JavaScript programmer ever since. Programmers started to find that although JScript looked just like JavaScript, some scripts would run in Netscape and not in Internet Explorer, and vice versa.
为了让它的JScript能支持到完整的JavaScript 1.1, 微软后来发布了IE3.0的第二
版. 由此掀开了JavaScript程序员在处理跨浏览器和跨浏览器不同版本之间的问题
的苦难生活. 程序员们发现虽然JScript看起来更象JavaScript了, 一些能在
Netscape里运行的脚本到IE里却不行了. 反之亦然.
Hoping to stave off chaos, Netscape and Sun looked for a third party to standardize JavaScript, and turned to the European Computer Manufacturers Association (ECMA, ). Unfortunately for programmers, that standardization, which began in November 1996, has been a long time coming, and even longer to be adopted. You might expect the standardized language to be called JavaScript, or possibly JScript, but it's not called either梚t's ECMAScript.
为了避免混乱, Netscape 和 Sun寻找第三方来标准化JavaScript, 他们瞄上了欧
洲计算机制造商协会(ECMA). 让程序员们遗憾的是, 这个开始于1996年11月的标准
化过程花了太多的时间, 然后又花了更多的时间才被人们采用. 你可能会想标准化
后的语言应该叫JavaScript或者可能是JScript, 但实际上却是ECMAScript.
So now we actually have three standards: JavaScript, JScript, and ECMAScript. Although ECMAScript has brought some measure of coherence to the picture, both browsers also have gone their own ways. The result is that the Internet Explorer often has two ways of letting you do things (the JScript way and the ECMAScript way), and the Netscape Navigator has two ways of letting you do things (the JavaScript way and the ECMAScript way).
所以现在我们实际上有三种标准: JavaScript, JScript和ECMAScript. 虽然
ECMAScript带来了一致性的度量标准, 两种浏览器却仍然各行其是. 结果就是IE经
常有两种方法让你做同一件事情(JavaScript式的和ECMAScript式的), Netscape的
浏览器也是一样(JavaScript式的和ECMAScript式的).
A big problem was that ECMAScript standardized the core JavaScript language itself, but not all the resources available in browsers that you work with梐nd those resources make up a much larger field than the JavaScript language does by itself.
一个很大的问题是ECMAScript虽然标准化了JavaScript的核心语言, 核心之外却留
下了很大的浏览器中可用的资源未被触及.
These browser resources are made up of browser objects (discussed later in this chapter). Each browser had its own set of objects, such as the document object, which enables you to work with the web page, and the window object, which enables you to work with the browser itself. A great deal of JavaScript programming is all about working with these objects; and if they're entirely different in the two browsers, it's easy to see that the same JavaScript-enabled web pages are going to have problems in one or the other browser梥o much so that people today are still coding different scripts in the same web page for the two browsers.
这些浏览器资源由浏览器对象组成(本章后面会讨论). 每个浏览器都有它自己的对象
集, 比如可以操作整个页面的文档对象和可以操作浏览器本身的窗口对象. 用
JavaScript编程的很大一部分工作誻跟这些对象打交道 ; 如果这些东西在两种浏
览器中完全不同的话, 那很容易出现这样的情况: 使用了JavaScript的页面在两者
之一中浏览时仍有问题, 所以时至今日仍有很多代码二套并存来分别对付两种浏览
器.
The chaos raised by different sets of browser objects threatened both JavaScript and JScript, so Microsoft, Netscape, and many other corporations went to the World Wide Web Consortium (W3C, ) to bring some coherence to the objects browsers would make accessible to JavaScript. The original browser object set, before W3C started its work, became known as the Document Object Model (DOM) "level 0." The DOM level 1, which became an official W3C specification on October 1, 1998, standardized many aspects of the objects found in browsers, but left out many others. DOM level 2 was made a specification on November 13, 2000, and fills in many more details. However, both DOM models omit any discussion of many of the popular objects available in both browsers, which means that much of what we're going to do is try to reconcile programming issues between those browsers. Work on the DOM level 3, now in the working draft stage, has started.
不同的浏览器对象集引起的混乱威胁着JavaScript和JScript, 所以微软,
Netscape和很多其它公司诉诸于万维网联盟(W3C, )来达成浏览器中对
象的对JavaScript的一致性. 在W3C开始他们的工作之前, 最初的浏览器对象集就
是文档对象模型(DOM)"第0级". 在1998年10月1日成为W3C官方标准的DOM Level 1
标准化了浏览器中对象的很多方面, 但也留下了很多未作定义. DOM Level 2标准
在2000年11月3日达成, 填补了很多细节. 然而, 两种DOM模型都略去了在两种浏览
器中一些很流行的对象的讨论, 这意味着我们还是要在两浏览器之间考虑一致性.
DOM的Level 3现在已经启动, 处在它的草案阶段.
The result of all this is that today, JavaScript is an amazingly powerful tool for web programmers, but it's still plagued by cross-browser issues. The current version of JavaScript in the Netscape Navigator is 1.5 (with 2.0 on the way), and the current JScript version is 5.6. With each new version, more power from other languages has been packed into JavaScript, borrowing from languages such as C++, Java, Perl, and even TCL.
对今天造成的结果就是JavaScript虽是Web编程的一种吸引人的强大工具, 但却要
遭受跨浏览器编程的痛苦问题. 在Netscape的浏览器中JavaScript的当前版是
1.5(2.0已经在做了), JScript的当前版本是5.6. 每一个新版本的发布, 都会有一
些来自其它语言如C++, Java, Perl甚至是TCL的有用特性被加入JavaScript.
Although the W3C DOM has brought some calm to the war between the browsers, it's also true that market share has dramatically shifted to Microsoft. It would be a shame if the only way JavaScript compatibility could finally be ensured were by the demise of one of the competitors, but it appears that might be happening. Nonetheless, we're going to work with both browsers in this book梚n fact, if there's an incompatibility, I'll indicate not only which browser something applies to, but which versions of that browser. That's the only way to do it.
虽然W3C的DOM为浏览器之争带来的一些平静, 事实上市场利益却极大地倾向于微软
. 如果最终解决JavaScript兼容性的方案是通过竞争者之一的消亡的话, 那可真是
讽刺, 但是看起来这正在变成事实. 不过, 在本书中我们会同时考虑两种浏览器,
实际上, 如果有不兼容的问题, 我不光会指出适用的浏览器, 也会指出具体的版本
. 这是唯一的办法.
We'll take a look at cross-browser programming in more detail both in this chapter and in Chapter 4, "Handling the Browser Environment." The very fact that incompatibility issues exist indicates that both Netscape and Microsoft consider JavaScript very important. And despite the cross-browser problems, JavaScript continues to flourish. So, in just what kind of ways do people use JavaScript these days? I'll take an overview of the JavaScript world now.
我们会在本章和第4章"处理浏览器环境"中详细研究跨浏览器编程的问题. 一个确
定的事实是兼容性的存在正说明Netscape和微软都认为JavaScript很重要. 而且即
使有着跨浏览器的问题存在, JavaScript却仍然日益兴旺. 那么, 今天的人们究竟
在用什么方式使用JavaScript呢? 现在我们来对JavaScript的世界来一个概览.
阅读(1185) | 评论(0) | 转发(0) |