[20141220]编写高质量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.30、理解prototype、getPrototypeOf和proto之间的不同

Tips:

  1. C.prototype属性是new C() 创建的对象的原型
  2. Object.getPrototypeOf(obj)是ES5中检索对象原型的标准函数
  3. obj.__ proto__是检索对象原型的非标准方法
  4. 类是由一个构造函数和一个关联的原型组成的一种设计模式

简单点说,就是prototype属性直接是创建的对象的原型;getPrototypeOf()是一个标准函数,来获取对象原型;而__ proto__则是不标准的原型属性。

//定义一个类型
function User(name, age){
this.name = name;
this.age = age;
}
//实例化类型
var user = new User('Jay', 23); //原型属性prototype作用在类对象上
User.prototype
//非标准__proto__作用在对象实例上
user.__proto__
//getPrototypeOf则是Object的一个方法,参数为实例对象
Object.getPrototypeOf(user) Object.getPrototypeOf(user) === User.prototype; // true
User.prototype === user.__proto__; // true

No.31、使用Object.getPrototypeOf()函数而不要使用__ proto__属性

Tips:

  1. 使用符合标准的Object.getPrototypeOf()函数而不要使用非标准的__ proto__属性
  2. 在支持__ proto__属性的非ES5环境中实现Object.getPrototypeOf()函数

由于非标准属性不具有完全兼容性,所以容易出一些奇奇怪怪的问题,不建议使用。 在支持__ proto__的非ES5标准环境下,使用下面代码来实现Object.getPrototypeOf()函数:

if(typeof Object.getPrototypeOf === 'undefined'){
Object.getPrototypeOf = function(obj){
var t = typeof obj;
if(!obj || (t !== 'object' && t !== 'function')){
throw new TypeError('Not an object.');
}
return obj.__proto__;
}
}

No.32、始终不要修改__ proto__属性

Tips:

  1. 始终不要修改__ proto__属性
  2. 使用Object.create函数给对象设置自定义原型

__ proto很特殊,具有修改对象原型链的能力。修改了 proto__属性可能会造成以下几个问题:

  1. 可移植性问题。并不是所有平台都支持改变对象原型的特性
  2. 性能问题。会使得引擎对JS代码的优化失效
  3. 行为不可预测。修改了__ proto__可能会破坏原有的继承体系

No.33、使构造函数和new操作符无关

Tips:

  1. 通过使用new操作符或Object.create方法在构造函数中调用自身使得该构造函数与调用语法无关
  2. 当一个函数期望使用new操作符调用时,清晰地文档化该函数

同31,我们来看一下User对象:

function User(name, age){
this.name = name;
this.age = age;
}
//如果使用new,那么会创建全新对象
var user = new User('Jay', 23); //如果忘记使用new呢?
var user = User('Jay', 23)
//这个时候,该句代码,相当于调用函数,此时this在一般情况下是window,在ES5严格模式下是undefined。
//当是window的时候,则会污染全局变量name和age,造成无法预期的问题。
//当是undefined的时候,则会直接导致一个即时错误。
//由于User没有显式return,导致等号左边的user的值为undefined。

为了避免以上问题,可能使用以下两种方式:

//方式一:
//通过在函数体判断,然后调用自身的方式来实现,一定会使用new。缺点是它需要额外的函数调用,对性能有影响。
function User(name, age){
if(!(this instanceof User)){
return new User(name, age);
}
this.name = name;
this.age = age;
} //方式二:
//通过判断this,将正确的接收者赋值给self,其他函数体内需要用this的地方,全部用self代替。缺点是使用了再ES5环境中有效的Object.create()。
function User(name, age){
var self = this instaceof User ? this : Object.create(User.prototype);
self.name = name;
self.age =age;
} //方式二补充,由于Object.create()只在ES5中生效,为了在旧环境中使用的话,可以使用以下方式扩充Object.create()。
if(typeof Object.create === 'undefined'){
Object.create = function(prototype){
function C(){}
C.prototype = prototype;
return new C();
}
}

No.34、在原型中存储方法

Tips:

  1. 将方法存储在实例对象中将创建该函数的多个副本,因为每个实例都有一份副本
  2. 将方法存储于原型中优于存储在实例对象中

将方法存储在原型上,那么多个实例对象会共享该原型方法。如果存储在实例上的,每创建一个实例则会创建一个函数副本,会占用更多的内存。

No.35、使用闭包存储私有数据

Tips:

  1. 闭包变量是私有的,只能通过局部引用获取
  2. 将局部变量作为私有数据从而通过方法实现信息隐藏

不多说,直接上代码:

function User(name, age){
// 私有对象
var privateObj = {
name: name,
age: age,
sex: '男'
}
// 公开属性
return {
name: privateObj.name,
age: privateObj.age,
setAge: function(age){
privateObj.age = age;
}
}
} var user = new User('Jay', 23);
console.log(user.name); // 'Jay'
console.log(user.age); // 23
console.log(user.sex); // undefined
user.setAge(25);
console.log(user.age); // 23

思考:为什么最后一个user.age 是 23???

修改如下呢:

function User(name, age){
// 私有对象
var privateObj = {
name: name,
age: age,
sex: '男'
}
// 公开属性
return {
name: privateObj.name,
age: function(){
return privateObj.age;
}
setAge: function(age){
privateObj.age = age;
}
}
}

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

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

    [20141227]编写高质量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. windows远程连接Linux(Ubuntu)的方法

    需要做的工作: 1.在Linux(Ubuntu)端安装.设置好SSH 2.下载putty,并通过putty的SSH连接登录Linux 一 .如何在Linux(Ubuntu)端安装.设置好SSH,获取I ...

  2. PHP之負載均衡下的session共用

    最近忙於開發台灣運動彩券第四版的程式,所以已經很久沒有上來寫東西了,今天隨便寫點東西和大家分享. 首先說一下負載均衡,相信大家都知道負載均衡可以很好地解決網站大流量的問題,負載均衡就是把用戶的請求分發 ...

  3. HTML解析组件HtmlAgilityPack使用

    HtmlAgilityPack是一个开源的解析HTML元素的类库,最大的特点是可以通过XPath来解析HMTL,如果您以前用C#操作过XML,那么使用起HtmlAgilityPack也会得心应手.目前 ...

  4. JS中的各种检测

    //null 只在肯定返回null值时才使用null比较 var element = document.getElementById("my-div"); if (element ...

  5. 【Android】android中Invalidate和postInvalidate的区别

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...

  6. 配置CENTOS YUM更新源

    众所周知,Centos 有个很方便的软件安装工具  yum,但是默认安装完centos,系统里使用的是国外的centos更新源,这就造成了我们使用默认更新源安装或者更新软件时速度很慢的问题. 为了使用 ...

  7. C++程序中调用MPI并行的批处理命令

    问题来源:在使用MPI时,将程序并行实现了,运行时需要在dos窗口下输入批处理命令,以完成程序的执行. 如:mpiexec -localroot -n 6 d:/mpi/pro.exe 但每次这样挺麻 ...

  8. Base: 一种 Acid 的替代方案

    原文链接: BASE: An Acid Alternative Pdf下载链接: Base 数据库 ACID,都不陌生:原子性.一致性.隔离性和持久性,这在单台服务器就能搞定的时代,很容易实现,但是到 ...

  9. css/js在线压缩工具

    http://tool.css-js.com/ 在进行前端的时候,可以参考百度性能监控中心给出的意见: http://developer.baidu.com/apm/index

  10. 机器学习技法--学习笔记03--Kernel技巧

    背景 上一讲从对偶问题的角度描述了SVM问题,但是始终需要计算原始数据feature转换后的数据.这一讲,通过一个kernel(核函数)技巧,可以省去feature转换计算,但是仍然可以利用featu ...