外文文献与翻译(共9页).doc

上传人:飞****2 文档编号:9100162 上传时间:2022-03-30 格式:DOC 页数:9 大小:44KB
返回 下载 相关 举报
外文文献与翻译(共9页).doc_第1页
第1页 / 共9页
外文文献与翻译(共9页).doc_第2页
第2页 / 共9页
点击查看更多>>
资源描述

《外文文献与翻译(共9页).doc》由会员分享,可在线阅读,更多相关《外文文献与翻译(共9页).doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上JavaScriptWhen I was a young journeyman programmer, I would learn about every feature of the languages I was using, and I would attempt to use all of those features when I wrote. I suppose it was a way of showing off, and I suppose it worked because I was the guy you went to if you want

2、ed to know how to use a particular feature.Eventually I figured out that some of those features were more trouble than they were worth. Some of them were poorly specified, and so were more likely to cause portability problems. Some resulted in code that was difficult to read or modify. Some induced

3、me to write in a manner that was too tricky and error-prone. And some of those features were design errors. Sometimes language designers make mistakes.Most programming languages contain good parts and bad parts. I discovered that I could be a better programmer by using only the good parts and avoidi

4、ng the bad parts. After all, how can you build something good out of bad parts?It is rarely possible for standards committees to remove imperfections from a language because doing so would cause the breakage of all of the bad programs that depend on those bad parts. They are usually powerless to do

5、anything except heap more features on top of the existing pile of imperfections. And the new features do not always interact harmoniously, thus producing more bad parts.But you have the power to define your own subset. You can write better programs by relying exclusively on the good parts.JavaScript

6、 is a language with more than its share of bad parts. It went from non-existence to global adoption in an alarmingly short period of time. It never had an interval in the lab when it could be tried out and polished. It went straight into Netscape Navigator 2 just as it was, and it was very rough. Wh

7、en Java applets failed, JavaScript became the Language of the Web by default. JavaScripts popularity is almost completely independent of its qualities as a programming language.Fortunately, JavaScript has some extraordinarily good parts. In JavaScript, there is a beautiful, elegant, highly expressiv

8、e language that is buried under a steaming pile of good intentions and blunders. The best nature of JavaScript is so effectively hidden that for many years the prevailing opinion of JavaScript was that it was an unsightly, incompetent toy. My intention here is to expose the goodness in JavaScript, a

9、n outstanding, dynamic programming language. JavaScript is a block of marble, and I chip away the features that are not beautiful until the languages true nature reveals itself. I believe that the elegant subset I carved out is vastly superior to the language as a whole, being more reliable, readabl

10、e, and maintainable.This book will not attempt to fully describe the language. Instead, it will focus on the good parts with occasional warnings to avoid the bad. The subset that will be described here can be used to construct reliable, readable programs small and large. By focusing on just the good

11、 parts, we can reduce learning time, increase robustness, and save some trees.Perhaps the greatest benefit of studying the good parts is that you can avoid the need to unlearn the bad parts. Unlearning bad patterns is very difficult. It is a painful task that most of us face with extreme reluctance.

12、 Sometimes languages are subsetted to make them work better for students. But in this case, I am subsetting JavaScript to make it work better for professionals.Why JavaScript?JavaScript is an important language because it is the language of the web browser. Its association with the browser makes it

13、one of the most popular programming languages in the world. At the same time, it is one of the most despised programming languages in the world. The API of the browser, the Document Object Model (DOM) is quite awful, and JavaScript is unfairly blamed. The DOM would be painful to work with in any lan

14、guage. The DOM is poorly specified and inconsistently implemented. This book touches only very lightly on the DOM. I think writing a Good Parts book about the DOM would be extremely challenging.JavaScript is most despised because it isnt SOME OTHER LANGUAGE. If you are good in SOME OTHER LANGUAGE an

15、d you have to program in an environment that only supports JavaScript, then you are forced to use JavaScript, and that is annoying. Most people in that situation dont even bother to learn JavaScript first, and then they are surprised when JavaScript turns out to have significant differences from the

16、 SOME OTHER LANGUAGE they would rather be using, and that those differences matter.The amazing thing about JavaScript is that it is possible to get work done with it without knowing much about the language, or even knowing much about programming. It is a language with enormous expressive power. It i

17、s even better when you know what youre doing. Programming is difficult business. It should never be undertaken in ignorance.Analyzing JavaScriptJavaScript is built on some very good ideas and a few very bad ones.The very good ideas include functions, loose typing, dynamic objects, and an expressive

18、object literal notation. The bad ideas include a programming model based on global variables.JavaScripts functions are first class objects with (mostly) lexical scoping. JavaScript is the first lambda language to go mainstream. Deep down, JavaScript has more in common with Lisp and Scheme than with

19、Java. It is Lisp in Cs clothing. This makes JavaScript a remarkably powerful language.The fashion in most programming languages today demands strong typing. The theory is that strong typing allows a compiler to detect a large class of errors at compile time. The sooner we can detect and repair error

20、s, the less they cost us. JavaScript is a loosely typed language, so JavaScript compilers are unable to detect type errors. This can be alarming to people who are coming to JavaScript from strongly typed languages. But it turns out that strong typing does not eliminate the need for careful testing.

21、And I have found in my work that the sorts of errors that strong type checking finds are not the errors I worry about. On the other hand, I find loose typing to be liberating. I dont need to form complex class hierarchies. And I never have to cast or wrestle with the type system to get the behavior

22、that I want.JavaScript has a very powerful object literal notation. Objects can be created simply by listing their components. This notation was the inspiration for JSON, the popular data interchange format. A controversial feature in JavaScript is prototypal inheritance. JavaScript has a class-free

23、 object system in which objects inherit properties directly from other objects. This is really powerful, but it is unfamiliar to classically trained programmers. If you attempt to apply classical design patterns directly to JavaScript, you will be frustrated. But if you learn to work with JavaScript

24、s prototypal nature, your efforts will be rewarded.JavaScript is much maligned for its choice of key ideas. For the most part, though, those choices were good, if unusual. But there was one choice that was particularly bad: JavaScript depends on global variables for linkage. All of the top-level var

25、iables of all compilation units are tossed together in a common namespace called the global object. This is a bad thing because global variables are evil, and in JavaScript they are fundamental. Fortunately, as we will see, JavaScript also gives us the tools to mitigate this problem.In a few cases,

26、we cant ignore the bad parts. There are some unavoidable awful parts, which will be called out as they occur. They will also be summarized in . But we will succeed in avoiding most of the bad parts in this book, summarizing much of what was left out in . If you want to learn more about the bad parts

27、 and how to use them badly, consult any other JavaScript book.The standard that defines JavaScript (aka JScript) is the third edition of The ECMAScript Programming Language. The language described in this book is a proper subset of ECMAScript. This book does not describe the whole language because i

28、t leaves out the bad parts. The treatment here is not exhaustive. It avoids the edge cases. You should, too. There is danger and misery at the edges.A JavaScript parser that can analyze a JavaScript program and report on the bad parts that it contains. JSLint provides a degree of rigor that is gener

29、ally lacking in JavaScript development. It can give you confidence that your programs contain only the good parts.JavaScript is a language of many contrasts. It contains many errors and sharp edges, so you might wonder, Why should I use JavaScript? There are two answers. The first is that you dont h

30、ave a choice. The Web has become an important platform for application development, and JavaScript is the only language that is found in all browsers. It is unfortunate that Java failed in that environment; if it hadnt, there could be a choice for people desiring a strongly typed classical language.

31、 But Java did fail and JavaScript is flourishing, so there is evidence that JavaScript did something right.The other answer is that, despite its deficiencies, JavaScript is really good. It is lightweight and expressive. And once you get the hang of it, functional programming is a lot of fun.But in o

32、rder to use the language well, you must be well informed about its limitations. I will pound on those with some brutality. Dont let that discourage you. The good parts are good enough to compensate for the bad parts.StyleComputer programs are the most complex things that humans make. Programs are ma

33、de up of a huge number of parts, expressed as functions, statements, and expressions that are arranged in sequences that must be virtually free of error. The runtime behavior has little resemblance to the program that implements it. Software is usually expected to be modified over the course of its

34、productive life. The process of converting one correct program into a different correct program is extremely challenging.Good programs have a structure that anticipatesbut is not overly burdened bythe possible modifications that will be required in the future. Good programs also have a clear present

35、ation. If a program is expressed well, then we have the best chance of being able to understand it so that it can be successfully modified or repaired.These concerns are true for all programming languages, and are especially true for JavaScript. JavaScripts loose typing and excessive error tolerance

36、 provide little compile-time assurance of our programs quality, so to compensate, we should code with strict discipline.JavaScript contains a large set of weak or problematic features that can undermine our attempts to write good programs. We should obviously avoid JavaScripts worst features. Surpri

37、singly, perhaps, we should also avoid the features that are often useful but occasionally hazardous. Such features are attractive nuisances, and by avoiding them, a large class of potential errors is avoided.The long-term value of software to an organization is in direct proportion to the quality of

38、 the codebase. Over its lifetime, a program will be handled by many pairs of hands and eyes. If a program is able to clearly communicate its structure and characteristics, it is less likely to break when it is modified in the never-too-distant future.JavaScript code is often sent directly to the pub

39、lic. It should always be of publication quality. Neatness counts. By writing in a clear and consistent style, your programs become easier to read.Programmers can debate endlessly on what constitutes good style. Most programmers are firmly rooted in what theyre used to, such as the prevailing style w

40、here they went to school, or at their first job. Some have had profitable careers with no sense of style at all. Isnt that proof that style doesnt matter? And even if style doesnt matter, isnt one style as good as any other?It turns out that style matters in programming for the same reason that it m

41、atters in writing. It makes for better reading.JavaScript当我还是一个初级程序员的时候,我想了解全部用到的语言的特点。 我写程序的时候会尝试着去应用所有的特性。我觉得这是炫耀的好办法,而我也确实得到了很多的荣耀,因为我对每个特性都非常的了解,每个人的问题我都能够解答。最终,我发现这些特性中有一部分带来的麻烦远远大于它们的价值。其中,一些特性是因为标准很不完整的原因,因而可能会导致可移植性的问题;一些特性会导致产生很难理解和修改的代码;一些特性使我们的代码风格过于复杂而且且很容易出错;还有一些特性就是设计的错误。大多数编程语言都有精华部分和

42、鸡肋部分。我发现如果只使用精华部分而避免使用鸡肋的部分这样可以使我成为一名更好的程序员。毕竟,用糟糕的部件怎么可能建出好的东西?标准委员会想要移除一门语言中的缺陷部分,这几乎是不可能的,因为这样做会损害所有依赖于那些部分的程序。除了在已存在的一大堆缺陷上堆积更多的特性,他们通常无能为力。并且新旧特性并不总是能和谐共处。可能从而产生出更多的部分。但是,你有权力定义你自己的子集。你完全可以基于精华部分去编写更好的程序。JavaScript中鸡肋部分的超出了预料。在短到令人吃惊的时间里,它从存在发展到全球采用。它从来没有在实验室里被试用和打磨。当它还非常粗糙时,它就被直接集成到网景的Navigato

43、r 2浏览器中。随着Java的小应用程序的失败,JavaScript变成了默认的网页语言。作为一门编程语言,JavaScript的流行几乎完全不受它的质量的影响。好在JavaScript有一些非常精华的部分。在JavaScript中,美丽的、优雅的、富有表现力的语言特性就像一些好的东西和一些不好的东西混在一起。JavaScript最本质的部分被常常地隐藏着,以至于多年来对它的主流观点是:JavaScript就是一个丑陋的、没用的东西。本书的目的就是要提示JavaScript中的精华,让大家知道它是一门杰出的动态编程语言。JavaScript就像是一块大理石,我要切除那些不好的特性直到这门语言的

44、真实的本质自我显露出来。我相信我精雕细琢出来的优秀的一小部分大大地优于这门语言的整体,它更可靠、更易读、更易于维护。我它将专注在精华部分上,同时会偶乐警告要去避免鸡肋的部分。这里将被描述的那部分可以用来构造可靠的、易读取的程序。通过仅专注于精华部分,我们就可以缩短学习时间,增强健壮性,并且还能拯救一些东西。或许只学习精华部分的最大好处就是你可以不用考虑的部分。忘掉不好的模式是非常困难的。这是一个非常痛苦的工作,我们中的大多数人都很不愿意面对。有时候,制定语言的子集是为了让学生更好地学习。但在这里,我的JavaScript子集是为了让专业人员更好了工作。为什么要使用JavaScriptJavaS

45、cript是一门重要的语言原因是它是web浏览器的语言。它与浏览器的结合使它成为世界上最流行的编程语言之一。同时,它也是世界上最被轻视的编程语言之一。浏览器的API和文档对象模型相当糟糕,导致JavaScript受到不公平的指责。在任何语言中处理DOM都是一件痛苦的事情,它的规范制定得很拙劣并且实现互不一致。JavaScript是最被轻视的语言,因为它不是所谓的主流语言。如果你擅长某些主流语言,但却在一个只能支持JavaScript的环境中编程,那么被迫使用JavaScript确是相当令人厌烦的事情。在那样的情形下,大多数人觉得没被要去学好JavaScript,但结果他们会惊讶地发现,Java

46、Script跟他们宁愿使用的主流语言有很大不同,而且这些不同点极为关键。JavaScript令人惊异的事情是,在对这门语言不太了解的情况下,甚至对编程都没有太多了解的情况下,你也可以用它来完成工作。编程是很困难的事情,绝对不应该在对一点都不了解时就开始工作。分析JavaScriptJavaScript建立在一些非常好的想法和少数非常坏的想法之上。那些非常好的想法包括函数、弱类型、动态对象和一个富有表现力的对象字面量表示法。那些坏的想法包括基于全局变量的编程模型。JavaScript的函数是基于词法作用域的顶级对象。JavaScript是第一个成为主流语言。实际上相对Java而言,JavaScr

47、ipt与Lisp和Scheme有更多的共同点。它是披着C外衣的lisp。这使得JavaScript成为一个非常强大的语言。现今大多数编程语言中都要求强类型。其原因在于强类型允许编译器在编译时就检测错误。我们能越早检测和更改错误,损失就会越小。JavaScript是一门弱类型的语言,所以JavaScript编译器不能检测和修改错误,这可能让从强类型语言转向JavaScript的开发人员感到惊恐。但事实证明,强类型并不会让你的测试工作变得轻松。并且我在工作中发现,强类型检查找到的那些错误并不是令我头痛的错误。另一方面,我发现弱类型自由的。我无须建立复杂的类层次,我永远不用做强制造型,也不用很难应付

48、类型系统以得到想得到的行为。JavaScript有非常强的对象字面量表示法。通过列出对象的组成部分,它们就能简单地被创建出来。这种表示法是促使我创立的数据交换格式的灵感。原型继承是JavaScript中一个有争议的特性。JavaScript有一个无类别的对象系统,在这个系统中,对象直接从其他对象继承属性。这真的很强大,但是对那些被训练使用类去创建对象的程序员们说,原型继承是一个陌生的概念。如果你尝试对JavaScript的原型本质,那么你的努力将会有所回报。JavaScript在关键思想的选择上饱受非议。虽然在大多数情况下,这些选择是合适的。但是有一个选择不合适;JavaScript依赖于全局

49、变量来进行连接。所有编译单元的所有顶级变量被整理到一个称为全局对象的公共命名中。这是一件不正确的事情,因为全局变量是魔鬼,并且在JavaScript中它们是基础性的。幸好,我们接下来能看到,JavaScript也给我们提供了解决这个问题的方法。在少数情况下,我们不能忽略的部分。另外还有一些不可避免的糟粕,当涉及这些部分时,我们将它们指出来。如果你想学习那些的部分及如何拙劣地使用它们,请参阅其它的JavaScript资料。这里的处理并没有穷尽,一些模棱两可的情况就没有处理。在实践中,你也应该这样。不确定确定的的情况会带来风险和麻烦事。JSLint的编程工具,它是一个JavaScript的解析器,它能分析JavaScript问题并且报告它包含的缺点。JSLint提供了一般在JavaScript开发中缺少的严密性。它能让你确信你的程序只包含优秀的部分。JavaScript是一门有许多差异的语言。它包含很多错误和尖锐的边角,所以你可能会疑惑

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 教育教学

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁