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']);

 

posted @ 2016-10-19 16:21  快饿死的鱼  阅读(2688)  评论(1编辑  收藏  举报