[20141030]编写高质量JS代码的68个有效方法(三)

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

No.11、熟练掌握闭包

Tips:

  1. 函数可以引用定义在其外部的作用域变量。
  2. 闭包比创建它们的函数有更长的生命周期。
  3. 闭包在内部存储其外部变量的引用,并能读写这些变量。

//第一个事实:JavaScript允许你引用在当前函数以外定义的变量。
function testClosures(){
var all = 'Test';
function test(m){
return all + ' and ' + m;
}
return test('closures');
}
testClosures(); //'Test and closures' //第二个事实:即使外部函数已返回,当前函数仍然可以引用在外部函数所定义的变量。
function testClosures(){
var all = 'Test';
function test(m){
return all + ' and ' + m;
}
return test;
}
var t = testClosures();
t('closures'); //'Test and closures' //第三个事实:闭包可以更新外部变量的值
function TestClass(){
var all;
return {
set: function(value){
all = value;
},
get: function(){
return all;
}
};
}
var t = new TestClass();
t.set('555');
t.get();

闭包的优缺点: 优点: 变量保护、封装性,能够实现字段的可访问性(示例如下)

function ModelClass(){
//Property
var name,age=23;
return {
setName: function(value){ //设置名称
name = value;
},
getName: function(){ //获取名称
return name;
},
getAge: function(){ //只读
return age;
}
};
}

缺点: 常驻内存,会增加内存使用量,使用不当和容易造成内存泄露。

No.12、理解变量申明提升

  1. 代码块中的函数申明会提升到函数顶部
  2. 重复申明变量被视为单个变量
  3. 考虑手动提升局部变量的申明,避免混淆(将函数内所需变量集中申明到函数顶部)

JavaScript支持词法作用域,而不支持块级作用域

function test(){
alert(a); //undefined
var a = 1;
alert(a); //1
}
test();
以上代码等价于:
function test(){
var a;
alert(a); //undefined
a = 1;
alert(a); //1
}
test();

一个例外是 try...catch :catch块中的变量作用域只在catch中。

function test(){
var x = '1';
try{
throw ''
}catch(x){
alert('error');
x = '2';
}
alert(x); // 1
}
test();

No.13、使用立即调用的函数表达式创建局部作用域

  1. 理解绑定与赋值的区别
  2. 闭包通过引用而不是值捕获它们的外部变量
  3. 使用立即调用的函数表达式(IIFE)来创建具有作用域
  4. 当心在立即调用的函数表达式中包裹代码块可能改变其行为的情形

看看以下代码段输出什么?

function test(){
var arr = [1,2,3,4,5];
var result = [];
for(var i = 0, len = arr.length; i < len; i++){
result[i] = function(){
return arr[i];
}
}
return result;
}
var result = test();
result[0]();

可以通过立即调用表达式来解决JavaScript缺少块级作用域。如上代码可修改为:

function test(){
var arr = [1,2,3,4,5];
var result = [];
for(var i = 0, len = arr.length; i < len; i++){
(function(){
var j = i;
result[i] = function(){
return arr[j];
}
})(i);
}
return result
}
var result = test();
result[0]();

No.14、当心命名函数表达式笨拙的作用域

  1. 在Error对象和调试器中使用命名函数表达式改进栈跟踪
  2. 在ES3和有问题的JS环境中,函数表达式作用域会被Object.prototype污染
  3. 谨记在错误百出的JS环境中会提升命名函数表达式声明,并导致命名函数表达式的重复存储
  4. 考虑避免使用命名函数表达式或在发布前删除函数名
  5. 如果将代码发布到正确实现的ES5的环境中,没什么好担心的

匿名和命名函数表达式的官方区别在于后者会绑定到与其函数名相同的变量上,该变量将作为该函数内部的一个局部变量。这可以用来写递归函数表达式。

var f = function find(tree, key){
if(!tree){
return null;
}
if(tree.key === key){
return tree.value;
}
//函数内部可以访问find
return find(tree.left, key) || find(tree.right, key);
}

结论:尽量避免使用命名函数表达式

No.15、当心局部块函数声明笨拙的作用域

  1. 始终将函数声明置于程序或被包含的函数的最外层以避免不可移植的行为
  2. 使用var声明和有条件赋值语句替代有条件的函数声明

function f(){
return 'global';
}
function test(x){
var result = [];
if(x){
function f(){
return 'local';
}
result.push(f());
}
result.push(f());
return result;
}
test(true);
test(false);

结论:尽量将函数块定义为变量,防止函数提前

编写高质量JS代码的68个有效方法(三)的更多相关文章

  1. 编写高质量JS代码的68个有效方法(八)

    [20141227]编写高质量JS代码的68个有效方法(八) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  2. 编写高质量JS代码的68个有效方法(七)

    [20141220]编写高质量JS代码的68个有效方法(七) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  3. 编写高质量JS代码的68个有效方法(六)

    [20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  4. 编写高质量JS代码的68个有效方法(四)

    [20141129]编写高质量JS代码的68个有效方法(四) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  5. 编写高质量JS代码的68个有效方法(二)

    [20141011]编写高质量JS代码的68个有效方法(二) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  6. JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)

    编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  7. 编写高质量JS代码的68个有效方法(十三)

    No.61.不要阻塞I/O事件队列 Tips: 异步API使用回调函数来延缓处理代价高昂的操作以避免阻塞主应用程序 JavaScript并发的接收事件,但会使用一个事件队列按序地处理事件处理程序 在应 ...

  8. 编写高质量JS代码的68个有效方法(十)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  9. 编写高质量JS代码的68个有效方法(十一)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. Android onTouch事件传递机制

    Android onTouch事件介绍: Android的触摸事件:onClick, onScroll, onFling等等,都是由许多个Touch组成的.其中Touch的第一个状态肯定是ACTION ...

  2. Hibernate入门4.核心技能

    Hibernate入门4.核心技能 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv 前言: 前面学习了Hibernate3的基本知识, ...

  3. Perl语言——简单说明

    Perl语言——简单说明 一.简单说明 Perl语言全称:实用摘录与报表语言|病态折中式垃圾列表器.Perl名称并不是缩写词,而是个溯写字. Perl语言历史:Larry Wall(拉里·沃尔)20世 ...

  4. Cocos2d-x3.x塔防游戏(保卫萝卜)从零开始(二)

    一.前提: 完成前一篇的内容. 具体参考:Cocos2d-x3.x塔防游戏(保卫萝卜)从零开始(一)篇 二.本篇目标: l  说说关于cocos2dx手机分辨率适配 l  对前一篇完成的塔防游戏原型进 ...

  5. mycat初探

    1:安装客户端 yum install mysql 2:安装服务端 yum install mysql-server 3:mycat要求不区分大小写 my.cnf(/etc/my.cnf)的[mysq ...

  6. Hadoop 2.7.1 使用minicluster进行单元测试

    目的是要跑通这个单元测试:hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestClose.java 这个Java文件比较简单: /** * Lic ...

  7. 解读Gartner《2015年度新兴技术成熟度曲线报告》

    详细见:http://www.360doc.com/content/16/0209/16/26186435_533443133.shtml 今年的报告评估了112个领域超过2000项新型技术的市场类型 ...

  8. 【WebMisCentral WMC】基于Extjs 4.2x的企业级用户授权认证中心系统(SSO+AM+SM),多租户SAAS应用

    http://saas.chinacloudtech.com 题记 三年磨一剑,在企业信息化的道路上已经走了3年之久了,3年多时间里做了很多,突破了很多:有无奈和辛酸,也有收货与喜悦:自我价值也在不断 ...

  9. SQL的主键和外键约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  10. 基于Multisim的buck降压斩波电路仿真

    和boost电路相反,buck电路为DC-DC降压电路,搞了一下仿真发现低压情况下降压效率比較高,电压略微高一点降压效率就不敢恭维了. .不知道是怎么回事 仿真电路: 占空比D设置了0.5, U0=U ...