1 / 12
文档名称:

TDD测试驱动的javascript开发------javascript的继承.doc

格式:doc   大小:51KB   页数:12页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

TDD测试驱动的javascript开发------javascript的继承.doc

上传人:用户头像没有 2017/7/13 文件大小:51 KB

下载得到文件列表

TDD测试驱动的javascript开发------javascript的继承.doc

相关文档

文档介绍

文档介绍:TDD测试驱动的javascript开发------ javascript的继承
说起面向对象,人们就会想到继承,常见的继承分为2种:接口继承和实现继承。接口继承只继承方法签名,实现继承则继承实际的方法。
由于函数没有签名,在ECMAScript中无法实现接口继承,只支持实现继承。
1. 原型链
原型链将作为实现继承的主要方法,基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。
构造函数---原型---实例之间的关系:
每一个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。
function SuperType() {
= true;
}
= function() {
return ;
};
function SubType() {
= false;
}
= new SuperType(); //通过原型链实现继承
= function() {
return ;
};
var subInstance = new SubType();
var superInstance = new SuperType();
TestCase("test extends",{

"test superInstance property should be true" : function() {
assertEquals(true,);
},
"test superInstance getSuperValue() should be return true" : function() {
assertEquals(true,());
},
"test subInstance property should be false" : function() {
assertEquals(false,);
},
"test subInstance could visit super method " : function() {
assertEquals(true,()); //SubType继承SuperType,并调用父类的方法
}
});
注:要区分开父类和子类的属性名称,否则子类的属性将会覆盖父类的同名属性值:看如下代码:
function SubType() {
= false;
}
= function() {
return ;
};
function SuperType() {
= true;
}
= function() {
return ;
};
= new SuperType(); //通过原型链实现继承
var subInstance = new SubType();
var superInstance = new SuperType();
TestCase("test extends",{

"test superInstance property should be true" : function() {
assertEquals(true,); //父类的property值为true
},
"test superInstance getSuperValue() should be return true" : function() {
assertEquals(true,()); //superInstance调用方法
},
"test subInstance property should be false" : function() {