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

[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;
        }
    }
}
时间: 2024-08-03 08:59:10

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

JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)

编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { marg

编写高质量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,

编写高质量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,

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

[20141011]编写高质量JS代码的68个有效方法(二) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table,

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

[20141129]编写高质量JS代码的68个有效方法(四) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table,

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

[20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table,

编写高质量JS代码的68个有效方法(十三)

No.61.不要阻塞I/O事件队列 Tips: 异步API使用回调函数来延缓处理代价高昂的操作以避免阻塞主应用程序 JavaScript并发的接收事件,但会使用一个事件队列按序地处理事件处理程序 在应用程序事件队列中绝不要使用阻塞的I/O JavaScript程序是构建在事件之上的.在其他一些语言中,我们可能常常会实现如下代码: var result = downFileSync('http://xxx.com'); console.log(result); 以上代码,如果downFileSyn

编写高质量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; } /* HEAD

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

No.21.使用apply方法通过不同数量的参数调用函数 Tips: 使用apply方法自定一个可计算的参数数组来调用可变参数的函数 使用apply方法的第一个参数给可变参数的方法提供一个接收者 //示例:计算给定数据的最大值 function getMaxNum(){ var max = arguments[0]; for(var i = 1, len = arguments.length;i < len; i++){ if(max < arguments[i]){ max = argume