最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript接口的實(shí)現(xiàn)三種方式(推薦)

 更新時(shí)間:2016年06月14日 14:38:07   作者:yujon  
這篇文章主要介紹了JavaScript接口的實(shí)現(xiàn)三種方式,有注釋法,檢查屬性法和鴨式辨行法,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧

Javascript模仿接口可以有三種方式:1.注釋法 2.檢查屬性法 3.鴨式辨形法

1.注釋法:此方法屬于程序文檔范疇,對(duì)接口的繼承實(shí)現(xiàn)完全依靠程序員自覺

/*
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)的接口方法添加到類屬性列表里,通過定義好的檢測(cè)反復(fù)檢查是否已經(jīng)實(shí)現(xiàn)了那些方法
//優(yōu)缺點(diǎn):可以強(qiáng)迫程序員實(shí)現(xiàn)接口,沒實(shí)現(xiàn)就報(bào)錯(cuò)。不過雖然聲明了自己實(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.鴨式辨型法:(不通過外表判斷鴨子,而通過其是否有鴨子的特性來判斷。如James Whitcomb Riley所說,像鴨子一樣走路并且嘎嘎叫的就是鴨子)

上面?zhèn)z種都聲明了自己實(shí)現(xiàn)了那些接口,其實(shí)聲明不重要,實(shí)現(xiàn)接口核心的是類實(shí)現(xiàn)了接口方法集。如果類具有了接口定義的所有方法函數(shù)名相同的函數(shù),那么認(rèn)為它實(shí)現(xiàn)了接口

//接口類,用來創(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']);

相關(guān)文章

最新評(píng)論

河津市| 尼勒克县| 莱西市| 蒙城县| 皋兰县| 钦州市| 福建省| 桂林市| 蒲城县| 古交市| 舟山市| 张家界市| 溆浦县| 卓尼县| 南靖县| 安西县| 宁武县| 芮城县| 安图县| 延庆县| 民勤县| 皮山县| 柯坪县| 黑水县| 灵宝市| 阜新市| 万州区| 满城县| 洱源县| 青州市| 旬邑县| 峨边| 兴业县| 文化| 龙游县| 黄梅县| 札达县| 建水县| 庄浪县| 夹江县| 遂溪县|