js实现接口的几种方式
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
js实现接⼝的⼏种⽅式
Javascript模仿接⼝可以有三种⽅式:1.注释法 2.检查属性法 3.鸭式辨形法
1.注释法:此⽅法属于程序⽂档范畴,对接⼝的继承实现完全依靠程序员⾃觉
/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){ //implements People interface
= name;
}
woman.prototype.showName = function(){
alert();
}
woman.prototype.createBody = function(){ //实现必要的⽅法
alert("⾝体已经创建好");
}
woman.prototype.createHead = function(){
alert("头部已经创建好");
}
//2.属性检查法:把要实现的接⼝⽅法添加到类属性列表⾥,通过定义好的检测反复检查是否已经实现了那些⽅法
//优缺点:可以强迫程序员实现接⼝,没实现就报错。
不过虽然声明了⾃⼰实现了哪些⽅法,但实现时很可能有遗漏
/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){
= name;
this.implementsInterfaces = ['People'];
}
woman.prototype.showName = function(){
alert();
}
woman.prototype.createBody = function(){ //实现必要的⽅法
alert("⾝体已经创建好");
}
woman.prototype.createHead = function(){
alert("头部已经创建好");
}
function implement(obj,interfaces){
for(var i=1;i<interfaces.length;i++){
var interfaceName = interfaces[i];
var interfaceFound = false;
for(var j=0;j<obj.implementsInterfaces.length;j++){
if(obj.implementsInterfaces[j] = interfaceName){
interfaceFound = true;
break;
}
}
if(!interfaceFound){
return false;
}
}
return true;
}
function isImplememts(instance,interfaces){ //判断对象是否已经继承相应接⼝
if(!implement(instance,interfaces)){
throw new Error("Object doesn't implement a required interface");
}
}
3.鸭式辨型法:(不通过外表判断鸭⼦,⽽通过其是否有鸭⼦的特性来判断。
如James Whitcomb Riley所说,像鸭⼦⼀样⾛路并且嘎嘎叫的就是鸭⼦)
上⾯俩种都声明了⾃⼰实现了那些接⼝,其实声明不重要,实现接⼝核⼼的是类实现了接⼝⽅法集。
如果类具有了接⼝定义的所有⽅法函数名相同的函数,那么认为它实现了接⼝
//接⼝类,⽤来创建接⼝
var Interface = function(name,motheds){
if(agruments.length!=2){
throw new Error("Interface constructor called with "+arguments.length+"arguments,but expected exactly 2");
}
= name;
this.methods = [];
for(var i=0;i<motheds.length;i++){
if(typeof motheds[i] !== 'string'){
throw new Error('Interface constructor expects mothed names to be'+'passes in as a string');
}
this.methods.push(motheds[i]);
}
}
Interface.prototype.ensureImplements = function(objs){
if(agruments.length != 1){
throw new Error("Interface constructor called with "+arguments.length+"arguments,but expected exactly 1")
}
for(var i=0;i<objs.length;i++){
var obj = objs[i];
for(var j=0;j<this.motheds.length;j++){
var mothed = this.methods[j];
if(!obj[mothed] || !typeof obj[mothed] !== 'function'){
throw new Error('Function Interface.ensureImplements:implements interface'++',obj.mothed'+mothed+'was not found'); }
}
}
}
//创建接⼝
var People = new Interface('People',['createHead','createBody']);
//⼦类
var Woman = function(name){
= name;
this.implementsInterfaces = ['People'];
}
Woman.prototype.showName = function(){
alert();
}
Woman.prototype.createBody = function(){ //实现必要的⽅法
alert("⼥⼈⾝体已经创建好");
}
Woman.prototype.createHead = function(){
alert("⼥⼈头部已经创建好");
}
//⼦类
var Man = function(name){
= name;
this.implementsInterfaces = ['People'];
}
Man.prototype.showName = function(){
alert();
}
Man.prototype.createBody = function(){ //实现必要的⽅法
alert("男⼈⾝体已经创建好");
}
Man.prototype.createHead = function(){
alert("男⼈头部已经创建好");
}
//判断是否实现
Poeple.ensureImplements(['Woman','Man']);。