编写高质量JS代码的68个有效方法(三)
[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:
- 函数可以引用定义在其外部的作用域变量。
- 闭包比创建它们的函数有更长的生命周期。
- 闭包在内部存储其外部变量的引用,并能读写这些变量。
//第一个事实: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、理解变量申明提升
- 代码块中的函数申明会提升到函数顶部
- 重复申明变量被视为单个变量
- 考虑手动提升局部变量的申明,避免混淆(将函数内所需变量集中申明到函数顶部)
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、使用立即调用的函数表达式创建局部作用域
- 理解绑定与赋值的区别
- 闭包通过引用而不是值捕获它们的外部变量
- 使用立即调用的函数表达式(IIFE)来创建具有作用域
- 当心在立即调用的函数表达式中包裹代码块可能改变其行为的情形
看看以下代码段输出什么?
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、当心命名函数表达式笨拙的作用域
- 在Error对象和调试器中使用命名函数表达式改进栈跟踪
- 在ES3和有问题的JS环境中,函数表达式作用域会被Object.prototype污染
- 谨记在错误百出的JS环境中会提升命名函数表达式声明,并导致命名函数表达式的重复存储
- 考虑避免使用命名函数表达式或在发布前删除函数名
- 如果将代码发布到正确实现的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、当心局部块函数声明笨拙的作用域
- 始终将函数声明置于程序或被包含的函数的最外层以避免不可移植的行为
- 使用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个有效方法(三)的更多相关文章
- 编写高质量JS代码的68个有效方法(八)
[20141227]编写高质量JS代码的68个有效方法(八) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(七)
[20141220]编写高质量JS代码的68个有效方法(七) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(六)
[20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(四)
[20141129]编写高质量JS代码的68个有效方法(四) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(二)
[20141011]编写高质量JS代码的68个有效方法(二) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)
编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- 编写高质量JS代码的68个有效方法(十三)
No.61.不要阻塞I/O事件队列 Tips: 异步API使用回调函数来延缓处理代价高昂的操作以避免阻塞主应用程序 JavaScript并发的接收事件,但会使用一个事件队列按序地处理事件处理程序 在应 ...
- 编写高质量JS代码的68个有效方法(十)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 编写高质量JS代码的68个有效方法(十一)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
随机推荐
- hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- FreeCodeCamp 前端初级算法(个人向)
freecodecamp 初级算法地址戳这里 Reverse a String 翻转字符串 function reverseString(str) { str=str.split("&quo ...
- Git版本工具的使用
Git版本工具:Git是一个开源的分布式版本控制系统,可用于敏捷高效的处理任何或大或小的项目.详细介绍地址:https://git-scm.com/downloads.今天主要为大家分享一下怎样把本地 ...
- 05管理登录名&服务器固定角色-大话数据库
大纲:学习如何利用SSMS快速自学T-SQL,先看看都有那些服务器固定角色,并且都是干啥的,如何把windows系统用户增加为登录名,单独新建登录名,修改登录名,删除登录名,将角色&登录名进行 ...
- IPv6正则表达式
斯蒂芬·瑞恩写了一个非常有用的正则表达式,可用于匹配任何一个合法的IPv6地址.以下为正则表达式的代码: ^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|: ...
- Codeforces Round #382 (Div. 2) D. Taxes 哥德巴赫猜想
D. Taxes 题目链接 http://codeforces.com/contest/735/problem/D 题面 Mr. Funt now lives in a country with a ...
- Metaweblog在Android上使用
同步发表于http://avenwu.net/2015/02/04/metaweblog metaweblog是一个博客接口协议,目前主流的博客平台均支持该协议,比如博客园,CSDN,WordPres ...
- 数据库中字段类型对应的C#中的数据类型
数据库中字段类型对应C#中的数据类型: 数据库 C#程序 int int32 text string bigint int64 binary System.Byte[] ...
- WebRTC is for Losers:WebRTC是输家
该文章是引述,仅代表作者Dave Michels观点 WebRTC is for Losers WebRTC technology has fallen short on many of its pr ...
- ffmpeg安装的问题
php语音转换需要安装ffmpeg文件 参考地址: http://thierry-xing.iteye.com/blog/2017864 http://diogomelo.net/blog/11/en ...