首页 技术 正文
技术 2022年11月6日
0 收藏 627 点赞 734 浏览 2920 个字

无意中看到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']);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287