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.屬性檢查法:把要實現的接口方法添加到類屬性列表裏,經過定義好的檢測反覆檢查是否已經實現了那些方法
//優缺點:能夠強迫程序員實現接口,沒實現就報錯。不過雖然聲明瞭本身實現了哪些方法,但實現時極可能有遺漏ui
/* 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所說,像鴨子同樣走路而且嘎嘎叫的就是鴨子)
上面倆種都聲明瞭本身實現了那些接口,其實聲明不重要,實現接口核心的是類實現了接口方法集。若是類具備了接口定義的全部方法函數名相同的函數,那麼認爲它實現了接口this
//接口類,用來建立接口 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']);