JavaScript接口的實現三種方式(推薦)
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("身體已經創(chuàng)建好"); } woman.prototype.createHead = function(){ alert("頭部已經創(chuàng)建好"); }
//2.屬性檢查法:把要實現的接口方法添加到類屬性列表里,通過定義好的檢測反復檢查是否已經實現了那些方法
//優(yōu)缺點:可以強迫程序員實現接口,沒實現就報錯。不過雖然聲明了自己實現了哪些方法,但實現時很可能有遺漏
/* 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("身體已經創(chuàng)建好"); } woman.prototype.createHead = function(){ alert("頭部已經創(chuàng)建好"); } 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所說,像鴨子一樣走路并且嘎嘎叫的就是鴨子)
上面?zhèn)z種都聲明了自己實現了那些接口,其實聲明不重要,實現接口核心的是類實現了接口方法集。如果類具有了接口定義的所有方法函數名相同的函數,那么認為它實現了接口
//接口類,用來創(chuàng)建接口 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'); } } } } //創(chuàng)建接口 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("女人身體已經創(chuàng)建好"); } Woman.prototype.createHead = function(){ alert("女人頭部已經創(chuàng)建好"); } //子類 var Man = function(name){ this.name = name; this.implementsInterfaces = ['People']; } Man.prototype.showName = function(){ alert(this.name); } Man.prototype.createBody = function(){ //實現必要的方法 alert("男人身體已經創(chuàng)建好"); } Man.prototype.createHead = function(){ alert("男人頭部已經創(chuàng)建好"); } //判斷是否實現 Poeple.ensureImplements(['Woman','Man']);
- 面向對象的Javascript之二(接口實現介紹)
- Javascript 面向對象(三)接口代碼
- JavaScript面向對象中接口實現方法詳解
- Javascript之面向對象--接口
- JavaScript接口實現代碼 (Interfaces In JavaScript)
- Javascript 面向對象(一)(共有方法,私有方法,特權方法)
- 淺談Javascript面向對象編程
- js面向對象之常見創(chuàng)建對象的幾種方式(工廠模式、構造函數模式、原型模式)
- javascript面向對象入門基礎詳細介紹
- 從面試題學習Javascript 面向對象(創(chuàng)建對象)
- Javascript 面向對象(二)封裝代碼
- JavaScript 接口原理與用法實例詳解
相關文章
Intellij中直接運行ts配置方法:run?configuration?for?typescript
run?configuration?for?typescript?插件本質還是依賴于ts-node來運行,只是其可以幫助我們自動配置好ts-node運行參數,簡化使用,這篇文章給大家介紹在Intellij中可以借助插件run?configuration?for?typescript直接運行typescript的方法,感興趣的朋友一起看看吧2023-08-08