




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精品文档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 wanted 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 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 avoiding 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 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 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. When 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 expressive 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, an 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, readable, 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 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. 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 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 language. 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 and 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 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 is 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 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 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 errors, 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. 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 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 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 JavaScripts 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 variables 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, 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 Appendix A. But we will succeed in avoiding most of the bad parts in this book, summarizing much of what was left out in Appendix B. If you want to learn more about the bad parts 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 it 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 generally 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 have 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. 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 order 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 made 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 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 presentation. 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 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. Surprisingly, 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 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 public. 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 where 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 matters in writing. It makes for better reading.JavaScript当我还是一个初级程序员的时候,我想了解全部用到的语言的特点。 我写程序的时候会尝试着去应用所有的特性。我觉得这是炫耀的好办法,而我也确实得到了很多的荣耀,因为我对每个特性都非常的了解,每个人的问题我都能够解答。最终,我发现这些特性中有一部分带来的麻烦远远大于它们的价值。其中,一些特性是因为标准很不完整的原因,因而可能会导致可移植性的问题;一些特性会导致产生很难理解和修改的代码;一些特性使我们的代码风格过于复杂而且且很容易出错;还有一些特性就是设计的错误。大多数编程语言都有精华部分和鸡肋部分。我发现如果只使用精华部分而避免使用鸡肋的部分这样可以使我成为一名更好的程序员。毕竟,用糟糕的部件怎么可能建出好的东西?标准委员会想要移除一门语言中的缺陷部分,这几乎是不可能的,因为这样做会损害所有依赖于那些部分的程序。除了在已存在的一大堆缺陷上堆积更多的特性,他们通常无能为力。并且新旧特性并不总是能和谐共处。可能从而产生出更多的部分。但是,你有权力定义你自己的子集。你完全可以基于精华部分去编写更好的程序。JavaScript中鸡肋部分的超出了预料。在短到令人吃惊的时间里,它从存在发展到全球采用。它从来没有在实验室里被试用和打磨。当它还非常粗糙时,它就被直接集成到网景的Navigator 2浏览器中。随着Java的小应用程序的失败,JavaScript变成了默认的网页语言。作为一门编程语言,JavaScript的流行几乎完全不受它的质量的影响。好在JavaScript有一些非常精华的部分。在JavaScript中,美丽的、优雅的、富有表现力的语言特性就像一些好的东西和一些不好的东西混在一起。JavaScript最本质的部分被常常地隐藏着,以至于多年来对它的主流观点是:JavaScript就是一个丑陋的、没用的东西。本书的目的就是要提示JavaScript中的精华,让大家知道它是一门杰出的动态编程语言。JavaScript就像是一块大理石,我要切除那些不好的特性直到这门语言的真实的本质自我显露出来。我相信我精雕细琢出来的优秀的一小部分大大地优于这门语言的整体,它更可靠、更易读、更易于维护。我它将专注在精华部分上,同时会偶乐警告要去避免鸡肋的部分。这里将被描述的那部分可以用来构造可靠的、易读取的程序。通过仅专注于精华部分,我们就可以缩短学习时间,增强健壮性,并且还能拯救一些东西。或许只学习精华部分的最大好处就是你可以不用考虑的部分。忘掉不好的模式是非常困难的。这是一个非常痛苦的工作,我们中的大多数人都很不愿意面对。有时候,制定语言的子集是为了让学生更好地学习。但在这里,我的JavaScript子集是为了让专业人员更好了工作。为什么要使用JavaScriptJavaScript是一门重要的语言原因是它是web浏览器的语言。它与浏览器的结合使它成为世界上最流行的编程语言之一。同时,它也是世界上最被轻视的编程语言之一。浏览器的API和文档对象模型相当糟糕,导致JavaScript受到不公平的指责。在任何语言中处理DOM都是一件痛苦的事情,它的规范制定得很拙劣并且实现互不一致。JavaScript是最被轻视的语言,因为它不是所谓的主流语言。如果你擅长某些主流语言,但却在一个只能支持JavaScript的环境中编程,那么被迫使用JavaScript确是相当令人厌烦的事情。在那样的情形下,大多数人觉得没被要去学好JavaScript,但结果他们会惊讶地发现,JavaScript跟他们宁愿使用的主流语言有很大不同,而且这些不同点极为关键。JavaScript令人惊异的事情是,在对这门语言不太了解的情况下,甚至对编程都没有太多了解的情况下,你也可以用它来完成工作。编程是很困难的事情,绝对不应该在对一点都不了解时就开始工作。分析JavaScriptJavaScript建立在一些非常好的想法和少数非常坏的想法之上。那些非常好的想法包括函数、弱类型、动态对象和一个富有表现力的对象字面量表示法。那些坏的想法包括基于全局变量的编程模型。JavaScript的函数是基于词法作用域的顶级对象。JavaScript是第一个成为主流语言。实际上相对Java而言,JavaScript与Lisp和Scheme有更多的共同点。它是披着C外衣的lisp。这使得JavaScript成为一个非常强大的语言。现今大多数编程语言中都要求强类型。其原因在于强类型允许编译器在编译时就检测错误。我们能越早检测和更改错误,损失就会越小。JavaScript是一门弱类型的语言,所以JavaScript编译器不能检测和修改错误,这可能让从强类型语言转向JavaScript的开发人员感到惊恐。但事实证明,强类型并不会让你的测试工作变得轻松。并且我在工作中发现,强类型检查找到的那些错误并不是令我头痛的错误。另一方面,我发现弱类型自由的。我无须建立复杂的类层次,我永远不用做强制造型,也不用很难应付类型系统以得到想得到的行为。JavaScript有非常强的对象字面量表示法。通过列出对象的组成部分,它们就能简单地被创建出来。这种表示法是促使我创立的数据交换格式的灵感。原型继承是JavaScript中一个有争议的特性。JavaScript有一个无类别的对象系统,在这个系统中,对象直接从其他对象继承属性。这真的很强大,但是对那些被训练使用类去创建对象的程序员们说,原型继承是一个陌生的概念。如果你尝试对JavaScript的原型本质,那么你的努力将会有所回报。JavaScript在关键思想的选择上饱受非议。
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年血液透析器项目申请报告
- 2025年美发师(高级)考试试卷:美发行业市场调研与竞争对手分析
- 2025年电脑提花人造毛皮机项目立项申请报告
- 我的宠物生活写物并抒情类作文14篇
- 2025年电工(高级技师)职业技能鉴定实操试卷:电气自动化技术技能案例分析
- 2025年安全生产管理工程师模拟试题
- 家庭经济情况与收入支出平衡证明(8篇)
- 清(梅)酒介绍试题
- 2025年旅游地产项目生态旅游规划与设计策略研究
- 2025年城市生活垃圾分类处理创新实践与公众教育体系研究报告001
- 胰岛素注射 课件
- 公司事故隐患内部报告奖励机制
- 【教育数字化应用案例】初中物理教育数字化应用案例
- 北京市西城区2021-2022学年八年级下学期期末历史试题(试题+答案)
- 土地综合整治项目施工组织设计
- 贵州省铜仁市2023-2024学年七年级下学期期末生物试题(解析版)
- 供应商定期评价表(精简版)
- HJ 620-2011 水质 挥发性卤代烃的测定 顶空气相色谱法
- 广西壮族自治区桂林市2023-2024学年七年级下学期期末考试数学试题
- 企业所得税汇算清缴申报表电子表格版(带公式-自动计算)
- 订婚解除婚约协议书模板
评论
0/150
提交评论