Javascript模仿接口可以有三種方式:1.注釋法 2.檢查屬性法 3.鴨式辨形法
1.注釋法:此方法屬於程序文檔范疇,對接口的繼承實現完全依靠程序員自覺
/* 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("頭部已經創建好"); }
//2.屬性檢查法:把要實現的接口方法添加到類屬性列表裡,通過定義好的檢測反復檢查是否已經實現了那些方法
//優缺點:可以強迫程序員實現接口,沒實現就報錯。不過雖然聲明了自己實現了哪些方法,但實現時很可能有遺漏
/* 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"); } }
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"); } 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']);