[20141129]编写高质量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.16、避免使用eval创建局部变量

Tips:

  1. 避免使用eval函数创建的变量污染调用者作用域。
  2. 如果eval函数代码可能创建全局变量,将此调用封装到嵌套的函数中已防止作用域污染。

执行eval时,eval中的变量才会被加到作用域中(函数作用域)

function fun1(){
eval('var y = 1;');
console.log('fun1->y:'+y); // 'fun->y:1'
}
fun1();
console.log('global->y:'+y); //throw Error

不要直接将不可控参数交给eval执行,可能会改变作用域对象。

//Bad code
var g = 'global';
function fun2(code){
eval(code);
}
fun2('var g="local"');
console.log(g) //'local' //Right code
var g = 'global';
function fun2(code){
(function(){
eval(code);
})();
}
fun2('var g="local"');
console.log(g) //'global',嵌套作用域

以上Right Code,如果执行不带var的变量申明,那么也是会影响全局的g对象的。

No.17、间接调用eval函数优于直接调用

Tips:

  1. 将eval函数同一个毫无意义的字面量包裹在序列表达式中以达到强制使用间接调用eval函数的目的
  2. 尽可能间接调用eval函数,而不要直接调用eval函数

直接调用eval,那么编译器无法优化JS代码。 如何间接调用eval?

(0,eval)(code)

No.18、理解函数的调用、方法调用及构造函数调用之间的不同

Tips:

  1. 方法调用将被查找方法属性的对象作用调用接收者
  2. 函数调用将全局对象作为其接受者。一般很少使用该函数调用语法来调用方法
  3. 构造函数需要通过new运算符调用,并产生一个新的对象作为其接收者

在全局对象上直接定义的function被称为函数,调用则是函数调用

var fun1 = function(p){
console.log(p);
}; function fun2(p){
console.log(p);
}
//函数调用
fun1('p1');
fun2('p2');

如果对象的属性是函数,那么称之为方法,使用模式则是方法调用

var obj = {
name: 'Hello ',
fun1: function(name){
console.log(this.name + name);
}
};
//方法调用
obj.fun1('Jay');

注意:fun1中通过this来访问obj的name属性

构造函数调用将一个全新的对象作为this变量的值

fucntion User(name, age){
this.Name = name;
this.Age = age;
}
//此时,user是一个全新的对象
var user = new User('Jay', 23);

No.19、熟练掌握高阶函数

Tips:

  1. 高阶函数是那些将函数作为参数或返回值的函数
  2. 熟练掌握现有库的高阶函数
  3. 学会发现可以被高阶函数所取代的常见编码模式

需求:将数组元素全部转换为大写

//常规做法
var arr = ['abc', 'test', '123'];
for(var i =0, len = arr.length; i < len; i++){
arr[i] = arr[i].toUpperCase();
}
console.log(arr); //高阶函数
var arr = ['abc', 'test', '123'];
arr = arr.map(function(item){
return item.toUpperCase();
});
console.log(arr);

注意:需要注意高阶函数使用时的返回值,有些是更改原始对象,有些是返回新对象

No.20、使用call方法自定义接收者来调用方法

Tips:

  1. 使用call方法自定义接收者(个人理解为作用域)来调用函数
  2. 使用call方法可以调用在给定对象中不存在的方法
  3. 使用call方法定义高阶函数允许使用者给回调函数指定接收者

function fun1(){
this.name = 'Test';
}
var obj = {
name: 'Jay'
};
console.log(obj.name);
fun1.call(obj);
console.log(obj.name);

call函数的调用方式:

f.call(obj, p1, p2, p3);

编写高质量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个有效方法(三)

    [20141030]编写高质量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. Ninx虚拟主机的配置

    1.配置ip ifconfig eth0 192.168.1.7 netmask 255.255.255.0 ifconfig eth0 192.168.1.17 netmak 255.255.255 ...

  2. nginx 配置其他路径

    gedit /etc/nginx/sites-enabled/default location /hlstest { types { application/vnd.apple.mpegurl m3u ...

  3. Putty & Ctrl+s 的魔咒

    Long long ago“ 某些旧的”哑终端“会在发送过来的数据太多,显示速度跟不上时发送一个Ctrl+s让对方等一下,然后再准备好继续显示时发送一个Ctrl+q.Putty“兼容”了这个特性.也有 ...

  4. 【字符串排序】n个数连接得到最小或最大的多位整数

    题目 描述:设有n个正整数,将它们依次连成在一排,组成一个多位数,现在要求可能组成的多位数中最大的多位数是什么? 例如:n=3时,3个整数13,312,343连成的最大多位数为:343-312-13. ...

  5. asp.net 多个文件同时下载

    1.首先读取文件夹下的文件,可能同时存在多个文件 2.选中文件,然后点击下载,同时可以选择多个文件. 思路:通过生产压缩包的形式进行下载,然后再清楚压缩包,这样用户可以一次性全部下载下来. 一.获取目 ...

  6. 表单验证Jquery扩展方法类

    /** 表单数据验证 **/ $.fn.Validform = function () { var Validatemsg = ""; var Validateflag = tru ...

  7. JAVA中类、实例与Class对象

    已同步更新至个人blog:http://dxjia.cn/2015/08/java-class-object/ 类 类是面向对象编程语言的一个重要概念,它是对一项事物的抽象概括,可以包含该事物的一些属 ...

  8. Java 或者android 的加密技术

    可以将Java文件编译之后得到的class文件(字节码)进行加密. 然后自定义一个classloader-类加载器,在载入class文件之后,对它进行解密,然后就可以正常运行了. 猜测,android ...

  9. 日暮·第一章·决斗

    日暮 第一章 决斗   泉州府,位于帝国的东南沿海,在数百年前,这里已是帝国最大的通商口岸之一,其一城之繁荣喧哗足以与异邦小国的都城相媲美,无数的人曾经来到这里,追逐财富,梦想,女人以及所有他们认为可 ...

  10. mysql相关问题

    MySQL导入.sql文件及常用命令,参考:http://blog.csdn.net/muziduoxi/article/details/6091202 修改mysql默认字符集的方法,参考:http ...