设计原则:
- 减少重复性代码
- 尽量弱化对象间的耦合
问题:让一个类继承另一个类会导致二者产生强耦合,即一个类依赖于另一类的内部实现.
javascript中的继承是基于对象的原型式继承,可以用来模仿基于类的类式继承.
一 伪类继承
通过用函数来声明类、用关键字new来创建实例。
var
Mammal
=
function
(name){
this .name = name;
}
Mammal.prototype.get_name = function (){
return this .name;
}
Mammal.prototype.says = function (){
return this .saying || '' ;
}
// 构造实例
var myMammal = new Mammal( ' Herb the Mammal ' );
var name = myMammal.get_name(); // Herb the Mammal
// 构造伪类继承Mammal
var Cat = function (name){
this .name = name;
this .saying = ' meow ' ;
};
Cat.prototype = new Mammal();
Cat.prototype.purr = function (n){
var i,s = '' ;
for (i = 0 ; i < n; i += 1 ){
if (s){
s += ' - ' ;
}
s += ' r ' ;
}
return s;
};
Cat.prototype.get_name = function (){
return this .says() + '' + this .name + '' + this .says();
};
var myCat = new Cat( ' Henrietta ' );
var says = myCat.says(); // 'meow'
var purr = myCat.purr( 5 ); // 'r-r-r-r-r'
var name = myCat.get_name(); // 'meow Henrietta meow'
this .name = name;
}
Mammal.prototype.get_name = function (){
return this .name;
}
Mammal.prototype.says = function (){
return this .saying || '' ;
}
// 构造实例
var myMammal = new Mammal( ' Herb the Mammal ' );
var name = myMammal.get_name(); // Herb the Mammal
// 构造伪类继承Mammal
var Cat = function (name){
this .name = name;
this .saying = ' meow ' ;
};
Cat.prototype = new Mammal();
Cat.prototype.purr = function (n){
var i,s = '' ;
for (i = 0 ; i < n; i += 1 ){
if (s){
s += ' - ' ;
}
s += ' r ' ;
}
return s;
};
Cat.prototype.get_name = function (){
return this .says() + '' + this .name + '' + this .says();
};
var myCat = new Cat( ' Henrietta ' );
var says = myCat.says(); // 'meow'
var purr = myCat.purr( 5 ); // 'r-r-r-r-r'
var name = myCat.get_name(); // 'meow Henrietta meow'
缺点:把一个应用拆解成一系列嵌套抽象类的分类过程.
二 原型继承:一个新对象可以继承一个旧对象的属性,通过构造一个对象,接着可构造更多类似的对象.
//
用对象字面量构造对象
var myMammal = {
name : ' Herb the Mammal ' ,
get_name : function (){
return this .name;
},
says : function (){
return this .saying || '' ;
}
};
// 利用Object.beget方法构造出更多实例
var myCat = Object.beget(myMammal);
myCat.name = ' Henrietta ' ;
myCat.saying = ' meow ' ;
myCat.purr = function (n){
var i,s = '' ;
for (i = 0 ; i < n; i += 1 ){
if (s){
s += ' - ' ;
}
s += ' r ' ;
}
return s;
};
myCat.get_name = function (){
return this .says() + '' + this .name + '' + this .says();
};
var says = myCat.says(); // 'meow'
var purr = myCat.purr( 5 ); // 'r-r-r-r-r'
var name = myCat.get_name(); // 'meow Henrietta meow'
var myMammal = {
name : ' Herb the Mammal ' ,
get_name : function (){
return this .name;
},
says : function (){
return this .saying || '' ;
}
};
// 利用Object.beget方法构造出更多实例
var myCat = Object.beget(myMammal);
myCat.name = ' Henrietta ' ;
myCat.saying = ' meow ' ;
myCat.purr = function (n){
var i,s = '' ;
for (i = 0 ; i < n; i += 1 ){
if (s){
s += ' - ' ;
}
s += ' r ' ;
}
return s;
};
myCat.get_name = function (){
return this .says() + '' + this .name + '' + this .says();
};
var says = myCat.says(); // 'meow'
var purr = myCat.purr( 5 ); // 'r-r-r-r-r'
var name = myCat.get_name(); // 'meow Henrietta meow'
三 new
javascript中的new运算符创建一个继承原型的新对象,同时this指针指向新对象,如果没有使用new,则得到普通的函数调用,且this指针指向全局对象,而不是新创建的对象.导致你初始化新成员元素时将会污染全局变量.而且编译时没有警告,运行时也没有警告.