js 鴨式辨型法


無意中看到arr.length === +arr.length;這句代碼,然后就去了解了下

這是一種鴨式辨型的判斷方法。

鴨式辨型:像鴨子一樣走路、游泳和嘎嘎叫的鳥就是鴨子

這句話表示:

  a.arr有length這個屬性

  b.arr.length是一個Number

那么arr可以是array,也可以是string

Jquery中要判斷一個變量是否是一個數組的確切方法:Object.prototype.toString.call(arr) === '[object Array]';

ES6可以通過 Array.isArray(arr)判斷數組

那么鴨式辨型一般用在什么地方呢?

  一般用在模擬接口的實現。、

js 沒有class 和 interface,但是Js的interface可以通過模擬來實現

模擬實現接口有三種方法:

  a.注釋法(該方法全憑程序自覺去繼承實現了)

/*
   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("頭部已經創建好");
}

  b.屬性檢測法/*

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"); }
}

  c.鴨式辨型法

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM