JavaScript接口的實(shí)現(xiàn)三種方式(推薦)
Javascript模仿接口可以有三種方式:1.注釋法 2.檢查屬性法 3.鴨式辨形法
1.注釋法:此方法屬于程序文檔范疇,對(duì)接口的繼承實(shí)現(xiàn)完全依靠程序員自覺(jué)
/* 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(){ //實(shí)現(xiàn)必要的方法 alert("身體已經(jīng)創(chuàng)建好"); } woman.prototype.createHead = function(){ alert("頭部已經(jīng)創(chuàng)建好"); }
//2.屬性檢查法:把要實(shí)現(xiàn)的接口方法添加到類屬性列表里,通過(guò)定義好的檢測(cè)反復(fù)檢查是否已經(jīng)實(shí)現(xiàn)了那些方法
//優(yōu)缺點(diǎn):可以強(qiáng)迫程序員實(shí)現(xiàn)接口,沒(méi)實(shí)現(xiàn)就報(bào)錯(cuò)。不過(guò)雖然聲明了自己實(shí)現(xiàn)了哪些方法,但實(shí)現(xiàn)時(shí)很可能有遺漏
/* 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(){ //實(shí)現(xiàn)必要的方法 alert("身體已經(jīng)創(chuàng)建好"); } woman.prototype.createHead = function(){ alert("頭部已經(jīng)創(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){ //判斷對(duì)象是否已經(jīng)繼承相應(yīng)接口 if(!implement(instance,interfaces)){ throw new Error("Object doesn't implement a required interface"); } }
3.鴨式辨型法:(不通過(guò)外表判斷鴨子,而通過(guò)其是否有鴨子的特性來(lái)判斷。如James Whitcomb Riley所說(shuō),像鴨子一樣走路并且嘎嘎叫的就是鴨子)
上面?zhèn)z種都聲明了自己實(shí)現(xiàn)了那些接口,其實(shí)聲明不重要,實(shí)現(xiàn)接口核心的是類實(shí)現(xiàn)了接口方法集。如果類具有了接口定義的所有方法函數(shù)名相同的函數(shù),那么認(rèn)為它實(shí)現(xiàn)了接口
//接口類,用來(lái)創(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(){ //實(shí)現(xiàn)必要的方法 alert("女人身體已經(jīng)創(chuàng)建好"); } Woman.prototype.createHead = function(){ alert("女人頭部已經(jīng)創(chuàng)建好"); } //子類 var Man = function(name){ this.name = name; this.implementsInterfaces = ['People']; } Man.prototype.showName = function(){ alert(this.name); } Man.prototype.createBody = function(){ //實(shí)現(xiàn)必要的方法 alert("男人身體已經(jīng)創(chuàng)建好"); } Man.prototype.createHead = function(){ alert("男人頭部已經(jīng)創(chuàng)建好"); } //判斷是否實(shí)現(xiàn) Poeple.ensureImplements(['Woman','Man']);
- 面向?qū)ο蟮腏avascript之二(接口實(shí)現(xiàn)介紹)
- Javascript 面向?qū)ο螅ㄈ┙涌诖a
- JavaScript面向?qū)ο笾薪涌趯?shí)現(xiàn)方法詳解
- Javascript之面向?qū)ο?-接口
- JavaScript接口實(shí)現(xiàn)代碼 (Interfaces In JavaScript)
- Javascript 面向?qū)ο螅ㄒ唬?共有方法,私有方法,特權(quán)方法)
- 淺談Javascript面向?qū)ο缶幊?/a>
- js面向?qū)ο笾R妱?chuàng)建對(duì)象的幾種方式(工廠模式、構(gòu)造函數(shù)模式、原型模式)
- javascript面向?qū)ο笕腴T基礎(chǔ)詳細(xì)介紹
- 從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對(duì)象)
- Javascript 面向?qū)ο螅ǘ┓庋b代碼
- JavaScript 接口原理與用法實(shí)例詳解
相關(guān)文章
js實(shí)現(xiàn)GIF動(dòng)圖分解成多幀圖片上傳
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)GIF動(dòng)圖分解成多幀圖片上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10js 客戶端打印html 并且去掉頁(yè)眉、頁(yè)腳的實(shí)例
下面小編就為大家?guī)?lái)一篇js 客戶端打印html 并且去掉頁(yè)眉、頁(yè)腳的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Intellij中直接運(yùn)行ts配置方法:run?configuration?for?typescript
run?configuration?for?typescript?插件本質(zhì)還是依賴于ts-node來(lái)運(yùn)行,只是其可以幫助我們自動(dòng)配置好ts-node運(yùn)行參數(shù),簡(jiǎn)化使用,這篇文章給大家介紹在Intellij中可以借助插件run?configuration?for?typescript直接運(yùn)行typescript的方法,感興趣的朋友一起看看吧2023-08-08IE瀏覽器打印的頁(yè)眉頁(yè)腳設(shè)置解決方法
IE瀏覽器打印的頁(yè)眉頁(yè)腳設(shè)置解決方法2009-12-12