首页 技术 正文
技术 2022年11月14日
0 收藏 785 点赞 4,596 浏览 7334 个字

[20141227]编写高质量JS代码的68个有效方法(八)

<!–
/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) */
/* Author: Nicolas Hery – http://nicolashery.com */
/* Version: b13fe65ca28d2e568c6ed5d7f06581183df8f2ff */
/* Source: https://github.com/nicolahery/markdownpad-github */

/* RESET
=============================================================================*/

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
}

/* BODY
=============================================================================*/

body {
font-family: Helvetica, arial, freesans, clean, sans-serif;
font-size: 14px;
line-height: 1.6;
color: #333;
background-color: #fff;
padding: 20px;
margin: 0 auto;
}

body>–>*: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);
});

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,989
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,503
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,131
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,842