[20141227]编写高质量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.36、只将实例状态存储在实例对象中

Tips:

  1. 共享可变数据可能会出问题,因为原型是被其所有的实例共享的
  2. 将可变的实例存储在实例对象中

一般来说,由于原型属性指向的对象是所有实例共享的。所以不建议在原型指向的对象中存储共享数据。下面给一个简单的例子:

var Person = function(name){
this.name = name;
};
Person.prototype = {
children: [],
addChild: function(childName){
this.children.push(childName);
},
getChildren: function(){
return this.children;
}
}; var p1 = new Person('P1');
var p2 = new Person('P2');
p2.addChild('P2_C1');
console.log(p1.getChildren());

结果比较明显。p2的孩子成p1的了。标准做法是将children存储在实例对象中。

var Person = function(name){
this.name = name;
this.children = [];
};
Person.prototype = {
addChild: function(childName){
this.children.push(childName);
},
getChildren: function(){
return this.children;
}
};

No.37、认识到this变量的隐式绑定问题

Tips:

  1. this变量的作用域总是有其最近的封闭函数所确定
  2. 使用一个局部变量(通常命名为self,me,that)使得this的绑定对于内部函数是可用的。

老规矩,看一个简单的示例:

var testObj = {
a1: 0,
fun1: function(){
function fun2(){
console.log(this.a1);
}
fun2();
}
};
testObj.fun1();

为什么会这样呢?因为this变量是以不同的方式被绑定的。每个函数都有一个this变量的隐式绑定。this变量是隐式的绑定到最近的封闭函数。针对以上的问题,可以有集中方法来处理,参考如下:

//通过将this用变量self保存的方式实现
var testObj = {
a1: 0,
fun1: function(){
var self = this;
function fun2(){
console.log(self.a1);
}
fun2();
}
};
testObj.fun1(); //通过call方法指定接收者(也可以用apply)
var testObj = {
a1: 0,
fun1: function(){
function fun2(){
console.log(this.a1);
}
fun2.call(this);
}
};
testObj.fun1(); //通过bind来实现
var testObj = {
a1: 1,
fun1: function(){
function fun2(){
console.log(this.a1);
}
fun2.bind(this)();
}
};
testObj.fun1();

No.38、在子类的构造函数中调用父类的构造函数

Tips:

  1. 在子类构造函数中显式地传入this作为显式的接收者调用父类的构造函数
  2. 使用Object.create函数来构造子类的原型对象以避免调用父类的构造

JS中实现的继承:

var Animal = function(){
this.weight = 50;
};
Animal.prototype.eat = function(){
console.log('eat food...');
}; var Dog = function(){
Animal.call(this);
Dog.prototype = Object.create(Animal.prototype);
}; var dog = new Dog();
console.log(dog.weight);

No.39、不要重用父类的属性名

Tips:

  1. 留意父类使用的所有属性名
  2. 不要再子类中重用父类的属性名

由于JS中,属性都是key-value存储,那么同名的属性指向同样的地址,所以以下代码:

var Animal = function(){
this.weight = 50;
this.id = ++Animal.nextId;
};
Animal.nextId = 0;
Animal.prototype.eat = function(){
console.log('eat food...');
}; var Dog = function(){
Animal.call(this);
this.id = ++ Dog.nextId;
Dog.prototype = Object.create(Animal.prototype);
};
Dog.nextId = 0; var dog = new Dog();
console.log(dog.id);

两个类都试图给实例属性id写数据。

No.40、避免继承标准类

Tips:

  1. 继承标准类往往会由于一些特殊的内部属性(如[[Class]])而被破坏
  2. 使用属性委托优于继承标准类

扩展标注库使得其功能更强大是很有诱惑力的,但不幸的是它们的定义具有很多特殊的行为,所以很难写出正确的子类。

var ArrayEx = function(){
for(var i = 0, len = arguments.length; i<len ; i++){
this[i] = arguments[i];
}
};
ArrayEx.prototype = Object.create(Array.prototype); var ar = new ArrayEx('1', '2');
console.log(ar.length) //猜猜结果是什么?

原因分析:length属性只对在内部标记为“真正的”数组对象才起作用。直接继承的对象并没有继承 Array的标记标签属性[[Class]]。测试如下:

var ar = new ArrayEx('1', '2');
console.log(Object.prototype.toString.call(ar)); //[object Object]
console.log(Object.prototype.toString.call([])); //[object Array]

ECMAScript标准库中干掉大多数构造函数都有类似的问题。基于这个原因,最好避免继承一下的标准类: Array,Boolean,Date,Function,Number,RegExp或String。

要想实现类似的功能,可以采用属性委托的方式:

var ArrayEx = function(){
this.array = []
for(var i = 0, len = arguments.length; i<len ; i++){
this.array[i] = arguments[i];
}
};
ArrayEx.prototype.forEach = function(f, thisArg){
if(typeof thisArg === 'undefined'){
thisArg = this;
}
this.array.forEach(f, thisArg);
}; var ar = new ArrayEx('1sfdfsd', '2fdsfs');
ar.forEach(function(item, i){
console.log(item);
});

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

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

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

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

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

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

    [20141129]编写高质量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. React组件系统、props与状态(state)

     多个组件合成一个组件: var style = { fontSize: 20, color: '#ff0000' }; var WebSite = React.createClass({ rende ...

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

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

  3. php: zend server 安装及相关配置

    运行安装文件(ZendServer-CE-php-5.3.2-5.0.1-Windows_x86.exe)开始安装,选项请参照我的选择. 这里不做改动,维持默认选择即可 点击Browse按钮更改安装目 ...

  4. [GO编程]GO编程环境

    GO是一个开源项目,由Google大神发明的,他主要是用于应用程序级开放,可以编译成机器码,和C++一样不需要.NET或JAVA那样的运行框架,因此是个不错的编程语言.更何况发明者都是高手中的高手,而 ...

  5. 模拟storage copy 功能失败的记录

    试验设想: 机器1: alter database begin backup 然后,在oracle仍然活动的状态下,将$ORACLE_BASE目录全部压缩拷贝到机器2 机器2: startup:失败 ...

  6. visual webgui theme designer

  7. mybatis 3.2.3 maven dependency pom.xml 配置

    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis< ...

  8. WPF Wonders: Transformations (and Robots!)

    indows Presentation Framework (WPF) gets a lot of mileage out of being layered on top of DirectX, in ...

  9. “合规性”是考核IT运维的重要指标

    ITSM的绩效考核向来是一个令人头疼的问题,有时就像一团乱麻,既无章可循,又无从下手.其实,只要掌握正确的思想方法,就能拨云见日.“斩乱麻”需“快刀”,“合规性考核”就是斩ITSM绩效考核这团乱麻的快 ...

  10. 关于BigDecimal的使用

    为什么使用BigDecimal 使用BigDecimal首先要注意到float,double是无法支持商业计算的.只能支持工程计算.即误差允许的计算.通常float占用4个字节,32位.double占 ...