[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. Python面试题(一)

    **晚上在公司的论坛上看到一道面试题,题目如下:随机给定一字符串和字符,要求重排,比如:'abde','c'.重排之后变成'abcde' **看到他们给的答案很多都是二分法重排,既然是字符类的处理,当 ...

  2. UWP开发-二维变换以及三维变换

    在开发中,由于某些需求,我们可能需要做一些平移,缩放,旋转甚至三维变换,所以我来讲讲在UWP中这些变换的实现方法. 一. 二维变换: UIElement.RenderTransform a.Trans ...

  3. Java数组一定要初始化才能使用吗?

    数组是大多数编程语言提供的一种复合结构,如果程序需要多个类型相同的变量时,就可以考虑定义一个数组.Java语言的数组变量是引用类型的变量,因此具有Java独有的特性. 在正常的Java开发中,使用Ja ...

  4. SqlServer2008R2用Windows身份登录18456错误解决

    // 重装系统后发现SqlServer2008R2使用Windows身份验证不能进行连接,如下图: 以前经常会碰到SqlServer 身份验证连接失败,Windows身份验证的连接失败还是第一次,试了 ...

  5. php内存缓存

    1.文件方式缓存 哈希子目录缓存 2.APC APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两 ...

  6. spring boot注解之@Scheduled定时任务实现

    java实现定时任务一般使用timer,或者使用quartz组件.现在在spring boot提供了更加方便的实现方式. spring boot已经集成了定时任务.使用@Secheduled注解. @ ...

  7. Linux安全事件应急响应排查方法总结

    Linux安全事件应急响应排查方法总结 Linux是服务器操作系统中最常用的操作系统,因为其拥有高性能.高扩展性.高安全性,受到了越来越多的运维人员追捧.但是针对Linux服务器操作系统的安全事件也非 ...

  8. mysql-5.7.15-winx64免安装版配置

    1.拷到硬盘根目录下; 2.在 bin 平行目录下新建  data 文件夹: 3. 修改  my-default.ini 文件,添加 basedir = C:\mysql-5.7.15-winx64d ...

  9. DataTable添加行和列数据

    protected void Page_Load() { DataTable newdtb = new DataTable(); newdtb.Columns.Add("Id", ...

  10. 第一章:Symfony2和HTTP基本原理

    恭喜你!通过学习Symfony2,你将用你自己的方式开发出更加高效.全面和流行的Web应用(当然,要受到用人单位或同行的欢迎,还是得靠你自己).Symfony2的存在是为了要解决最根本的问题:即提供一 ...