编写可维护的JavaScript
最新书摘:
-
王焱燚Rex2018-06-14一般来讲,在下面这些场景中添加空行也是不错的主意。·在方法之间·在方法中的局部变量(local variable)和第一条语句之间·在多行或单行注释之前·在方法内的逻辑片段之间插入空行,提高可读性
-
王焱燚Rex2018-06-141)Java语言编程规范中规定源码里单行长度不超过80个字符,文档中代码单行长度不超过70个字符。2)Android开发者编码风格指南规定单行代码长度不超过100个字符。3)非官方的Ruby编程规范中规定单行代码长度不超过80个字符。4)Python编程规范中规定单行代码长度不超过79个字符。
-
王焱燚Rex2018-06-13我推荐使用4个空格字符为一个缩进层级。很多文本编辑器都默认将缩进设置为4个空格,你可以在编辑器中配置敲入Tab键时插入4个空格。使用2个空格的缩进时,代码的视觉展现并不是最优的,至少看起来是这样。尽管是选择制表符还是选择空格做缩进只是一种个人偏好,但绝对不要将两者混用,这非常重要。这么做会导致文件的格式很糟糕,而且需要做不少清理工作。
-
王焱燚Rex2018-06-13我们会经常碰到这两个术语:“编程风格”(style guideline)和“编码规范”(code convention)。编程风格是编码规范的一种,用来规约单文件中代码的规划。编码规范还包含编程最佳实践、文件和目录的规划以及注释等方面。
-
castle2015-06-01This is a good and appropriate use of feature detection, because the code tests for afeature and then, if it’s there, uses it. The test for document.getElementById() comesfirst because it is the standards-based solution. After that come the two browser-specificsolutions. If none of these features is available, then the method simply returns null.The best part about this function is that when Internet Explorer 5 and Netscape 6 werereleased with support for document.getElementById(), this code didn’t need to change.The previous example illustrates several important parts of good feature detection:1. Test for the standard solution2. Test for browser-specific solutions3. Provide a logical fallback if no solution is available
-
castle2015-06-01var person = {name: "Nicholas"};delete person.name;console.log(person.name); // undefinedThis example removes the name property from the person object. The delete operatorworks only on instance properties and methods. If delete is used on a prototype propertyor method, it has no effect. For example:// No effectdelete document.getElementById;console.log(document.getElementById("myelement")); // stil works
-
castle2015-06-01// Internet Explorer 8 and earlierconsole.log(typeof document.getElementById); // "object"console.log(typeof document.createElement); // "object"console.log(typeof document.getElementsByTagName); // "object"This quirk arises due to how the browser implements the DOM. In short, these earlyversions of Internet Explorer didn’t implement the DOM as native JavaScript functions,which caused the native typeof operator to identify the functions as objects. Becausethe DOM is so well defined, developers typically test for DOM functionality using thein operator, understanding that the presence of the object member means that it’s afunction, as in:// detect DOM methodif ("querySelectorAll" in document) {images = document.querySelectorAll("img");}This code checks to see whether querySelec...
-
castle2015-06-01Reference values are also known as objects. In JavaScript, any value that isn’t a primitiveis definitely a reference.
-
castle2015-06-01The variable YourGlobal can actually have any name. The important part is the namespace() method, which nondestructively creates namespaces based on the string thatis passed in and returns a reference to the namespace object. Basic usage:/** Creates both YourGlobal.Books and YourGlobal.Books.MaintainableJavaScript.* Neither exists before hand, so each is created from scratch.*/YourGlobal.namespace("Books.MaintainableJavaScript");// you can now start using the namespaceYourGlobal.Books.MaintainableJavaScript.author = "Nicholas C. Zakas";/** Leaves YourGlobal.Books alone and adds HighPerformanceJavaScript to it.* This leaves YourGlobal.Books.MaintainableJavaScript intact.*/YourGlobal.namespace("Books.HighPerformanceJavaScript");// still a valid referenceconsole.log(YourGloba...
-
rayliao2014-04-27注释是和元素及文本一样的DOM节点,因此可以通过javascript将其提取出来。
-
小小的寂寞2013-11-17译注: JavaScript 权威指南 (第六版) 3.6 “包装对象” 详细解释了包装对象和包装类型的细节,需要尤为注意的一点是,原始值本身不具有对象特性,比如 1.toString() 是报错的,必须这样做: var a=1; a.toString()
-
dhcn2013-10-14构建软件设计的方法有两种:一种是把软件做得很简单以至于明显找不到缺陷;另一种是把它做得很复杂以至于找不到明显的缺陷。
-
dhcn2013-08-29计算机科学只存在两个难题:缓存失效和命名。------------Phil Karlton
-
胡涛2013-05-01所有的var语句都提前到包含这段逻辑的函数的顶部执行。
-
胡涛2013-05-01理解null的最好方式就是将它当做对象的占位符
-
寸志2012-12-25It’s always best to split application logic from any event handler, because the same logicmay need to be triggered by different actions in the future.
-
寸志2012-12-25Rule #1: Separate Application Logic
-
豆豆爸爸2012-07-13“There are two ways of constructing a software design: One way is to make it so simplethat there are obviously no deficiencies and the other way is to make it so complicatedthat there are no obvious deficiencies.” —C.A.R. Hoare, The 1980 ACM Turing AwardLecture
-
王焱燚Rex2018-09-04尽管语法相似,JavaScript中的switch语句的行为和在其他语言中国呢是不一样的:switch语句中可以使用任意类型值,任何表达式都可合法地用于case语句。
-
王焱燚Rex2018-09-04不论块语句(block statement)包含多行代码还是单行代码,都应当总是使用花括号。