编写高质量JS代码的68个有效方法(八)
[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:
- 共享可变数据可能会出问题,因为原型是被其所有的实例共享的
- 将可变的实例存储在实例对象中
一般来说,由于原型属性指向的对象是所有实例共享的。所以不建议在原型指向的对象中存储共享数据。下面给一个简单的例子:
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:
- this变量的作用域总是有其最近的封闭函数所确定
- 使用一个局部变量(通常命名为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:
- 在子类构造函数中显式地传入this作为显式的接收者调用父类的构造函数
- 使用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:
- 留意父类使用的所有属性名
- 不要再子类中重用父类的属性名
由于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:
- 继承标准类往往会由于一些特殊的内部属性(如[[Class]])而被破坏
- 使用属性委托优于继承标准类
扩展标注库使得其功能更强大是很有诱惑力的,但不幸的是它们的定义具有很多特殊的行为,所以很难写出正确的子类。
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个有效方法(八)的更多相关文章
- 编写高质量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个有效方法(三)
[20141030]编写高质量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; } /* ...
随机推荐
- 今日例子border
border这个属性在页面上的使用率还是很高,例如我们需要理解的盒模型就需要对border有个 比较深的理解,如果你会盒模型,但对border没有深的理解,那只能说你只是知道盒模型,而 不是懂得盒模型 ...
- Ninja Blocks物联网平台简介
Ninja Blocks是一个物联网控制平台,其平台架构包括硬件层.处理器层.软件层以及平台层,请看下图: 最底层是硬件层,包括传感器(Sensors)和驱动器(Actuators),例如温度传感器. ...
- android: SQLite创建数据库
SQLite 是一款轻量级的关系型数据库,它的运算速度非常快, 占用资源很少,通常只需要几百 K 的内存就足够了,因而特别适合在移动设备上使用.SQLite 不仅支持标准的 SQL 语法,还遵循了数据 ...
- Objective-c中 isEqual ,isEqualToString , == 三者的区别
首先 OC中的对象都是用指针表示,方法的调用是基于消息机制实现,== 比较的自然是指针指向的地址 然后 说下 isEqual 和 isEqualToString 的区别 IsEqual 是 NSObj ...
- informix 查看 当前锁表
select username,sid,waiter,dbsname,tabname,rowidlk,keynum,type from sysmaster:syslocks l, sysmaster: ...
- Window Server 2008 R2 TFS2010的安装和配置
1.打开Setup进行安装 2.下一步,然后功能全选 3.点击安装,便开始安装了 安装成功 配置 进行配置之后,选择高级,因为其他功能可能没那么多 到如下界面后,直接进行下一步就可以 下一步,设置TF ...
- Advacned Puppet: Puppet Master性能调优
本文是Advanced Puppet系列的第一篇:Puppet master性能调优,谈一谈如何优化和提高C/S架构下master端的性能. 故事情节往往惊人地类似:你是一名使用Puppet管理线上业 ...
- FoxMail的Bug
Foxmail 7.2 build6.040,win7中文专业版 下载腾讯的企业邮箱的邮件, 自动配置为imap收邮件 收件箱应该为1740封 邮件 实际foxmail却只收到1500多封 改成pop ...
- Scala 深入浅出实战经典 第43讲:主要介绍类型变量bound
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- C# inline-hook / api-hook
我查阅了一下相关C#方面的资料,却没有发现有提供过关于api-hook方面的资 料包括应用库由此本人编写一套inline-hook的库用于支持x64.x86上的基于在 clr的公共语言,如: c#.c ...