JavaScript实现接口的三种方式详解

作者:栖木

时间:2026-10-30

浏览:642

本文详细介绍JavaScript中实现接口的三种常见方式:注释法、属性检查法和鸭式辨形法。通过对比分析,重点讲解如何利用Interface类进行严格的方法名与数量校验,确保对象实现接口定义的所有方法,从而提升代码的健壮性与可维护性。

文章目录

JavaScript接口实现的三种策略概述

在JavaScript中,模拟接口实现主要有三种策略:第一种是注释法,第二种是属性检查法,第三种是鸭式辨形法。

注释法:依赖自觉的文档策略

第一种策略为注释法。这种方法主要属于程序文档的范畴,其对接口的继承与实现完全依赖于程序员的自觉性与规范性,缺乏强制性的技术约束。


/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){ //implements People interface
this.name = name;
}
woman.prototype.showName = function(){
alert(this.name);
}
woman.prototype.createBody = function(){ //实现必要的方法
alert("身体已经创建好");
}
woman.prototype.createHead = function(){
alert("头部已经创建好");
} 

属性检查法:基于验证的强制实现

第二种策略为属性检查法。该方法的核心思路是将需要实现的接口方法名添加到类的属性列表中,并通过预先定义好的检测逻辑反复验证这些方法是否已实现。其优缺点在于:虽然能够强迫程序员实现接口,若未实现则抛出错误;但尽管声明了所实现的接口方法,实际编码时仍可能出现遗漏情况。


/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
woman.prototype.showName = function(){
alert(this.name);
}
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");
}
} 

鸭式辨形法:基于特性的动态判断

第三种策略为鸭式辨形法。该理念源自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");
}
this.name = 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'+this.name+',obj.mothed'+mothed+'was not found');
}
}
}
}
//创建接口
var People = new Interface('People',['createHead','createBody']);
//子类
var Woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Woman.prototype.showName = function(){
alert(this.name);
}
Woman.prototype.createBody = function(){ //实现必要的方法
alert("女人身体已经创建好");
}
Woman.prototype.createHead = function(){
alert("女人头部已经创建好");
}
//子类
var Man = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Man.prototype.showName = function(){
alert(this.name);
}
Man.prototype.createBody = function(){ //实现必要的方法
alert("男人身体已经创建好");
}
Man.prototype.createHead = function(){
alert("男人头部已经创建好");
}
//判断是否实现
Poeple.ensureImplements(['Woman','Man']);

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章