目录

instanceof

constructor

构造函数名字

鸭式辨型

三种检测对象的类方式: instanceof、constructor 、构造函数名字

用法如下:

1)instanceof

console.log([1,2,3] instanceof Array);
true console.log([1,2,3] instanceof Object);
true

尽管构造函数是原型的唯一标识,instanceof运算符的右操作数是构造函数,instanceof实际计算过程中检测的是对象的继承关系,而不是检测的创建对象时的构造函数,只是使用了构造函数作为中介

当然也可以使用isPrototypeOf 来判断一个对象是否存在于另一对象的原型链中,此时不使用构造函数作为中介

var a1 = new Array();
console.log(Array.prototype.isPrototypeOf(a1));
true console.log(Array.prototype.isPrototypeOf([1,2,3]));
true

注意:多个执行上下文时(例如存在不同框架时)instanceof使用有限制

2)constructor

每个javascript函数都可以用作构造函数,调用构造函数需要使用prototype属性,因而每个javascript函数会自动拥有prototype属性,这个属性值是一个对象,这个对象包含一个contructor属性,constructor属性值是一个函数对象。

即对于函数var F = function(){}; F.prototype.constructor===F

关系图如下:

eg:

var F= function(){};
var p = F.prototype;
var c = p.constructor;
console.log(p);
console.log(c);
console.log(c===F);
Object {}
function (){}
true

因而对象继承的constructor均指代他们的构造函数

eg:

var o= new F();
console.log(o.constructor===F);
//输出 
true
var a = new Array();
console.log(a.constructor===Array);
//输出
true
function typeDiscern(x){
switch(x.constructor){
case Number: return "Number:"+x;
case String: return "String:"+x;
case Array: return "Array:"+x;
}
}
console.log(typeDiscern([1,2,3]));
console.log(typeDiscern("abc"));
console.log(typeDiscern(5));
//输出
Array:1,2,3
String:abc
Number:5

注意: 同instanceof在多个上下文下没法使用,另外并不是所有的对象都包含constructor属性

eg:

定义Person类

function Person(name)
{
this.name=name;
this.getName=function()
{
return this.name;
}
}; var wish=new Person('js'); console.log(wish.constructor==Person);
console.log(Person.prototype);
console.log(Person.constructor);
console.log(wish.getName()); //输出
true
Person {}
function Function() { [native code] }
js

给Person自定义prototype

function Person(name)
{
this.name=name;
this.getName=function()
{
return this.name;
}
};
Person.prototype={
toString: function(){
return this.name;
}
}; var wish=new Person('js'); console.log(wish.constructor==Person);
console.log(Person.prototype);
console.log(Person.constructor);
console.log(wish.getName());
console.log(wish.toString()); //输出
false
Object {toString: function}
function Function() { [native code] }
js
js

此时新定义的原型对象不含有constructor属性,因而Person的实例也不包含constructor属性

解决方法:可显示的给原型添加构造方法

Person.prototype={
constructor=Person,
toString: function(){
return this.name;
}
};

构造函数名字

没有intanceof和constructor的执行上下文问题,一个窗口中的Array构造函数和另一个窗口内Array构造函数不等,但是构造函数名字相同,但是并不是每个函数都有名字

Function.prototype.getName= function(){
if("name" in this){
return this.name;
}
return this.name=this.toString().match(/function\s*([^(]*)/);
}
function test1(){
}
console.log(test1.getName()); //输出:
test1

鸭式辨型

关注对象能做什么,而不是对象的类是什么

James Whitcomb Riley提出像鸭子一样走路、游泳和嘎嘎叫的鸟就是鸭子

主要对象包含walk(),swim(),bike()这三个方法就可以作为参数传入

利用鸭式辩型实现的函数:

function quackImplements(o/*,...*/){
for(var i=1; i<arguments.length;i++){
var arg=arguments[i];
switch(typeof arg){
case 'string':
if(typeof o[arg]!=="function")
return false;
continue; case 'function':
arg=arg.prototype; case 'object':
for (var m in arg){
if(typeof arg[m]!=="function") continue;
if(typeof o[m]!=="function") return false; }
}
}
return true;
}

对于字符串直接检查命名方法

对于对象检查是否有同名方法

对于函数检查构造函数的原型对象中是否有相同方法

在javascript中很多函数都不对对象做类型检测只是关心这些对象能做什么

eg:Array的prototype利用了鸭式辨型,arguments是伪数组

 (function () {
var arr = Array.prototype.slice.apply(arguments);
console.log(arr);
})(1, 2, 3); //输出:
[1, 2, 3]
 var arr = Array.prototype.slice.apply({ 0: 1, 1: 2, 2: 3, length: 3 });
console.log(arr); //输出:
[1, 2, 3]

使用鸭式辨型可以扩大对象的使用范围

eg:让普通对象具有数组的push方法

Function.prototype.unCurrying = function () {
var f = this;
return function () {
var a = arguments;
return f.apply(a[0], [].slice.call(a, 1));
};
};
Function.prototype.unCurrying = function () {
return this.call.bind(this);
}; var push = Array.prototype.push.unCurrying(),
obj = {};
push(obj, 'first', 'two');
console.log(obj);
console.log("length:"+obj.length)

输出:

Object{0: "first", 1: "two", length: 2}
length:2

参考:javascript权威指南

    http://www.cnblogs.com/pigtail/p/3450852.html

js类型判断及鸭式辨型的更多相关文章

  1. js 鸭式辨型法

    无意中看到arr.length === +arr.length;这句代码,然后就去了解了下 这是一种鸭式辨型的判断方法. 鸭式辨型:像鸭子一样走路.游泳和嘎嘎叫的鸟就是鸭子 这句话表示: a.arr有 ...

  2. 类型和原生函数及类型转换(二:终结js类型判断)

    typeof instanceof isArray() Object.prototype.toString.call() DOM对象与DOM集合对象的类型判断 一.typeof typeof是一个一元 ...

  3. JS类型判断&原型链

    JS类型检测主要有四种 1.typeof Obj 2.L instanceof R 3.Object.prototype.toString.call/apply(); 4.Obj.constructo ...

  4. js类型判断-丰富加好用

    一, 自己有时候写一些东西,要做类型判断,还有测试的时候,对于原生的和jQuery中的类型判断,实在不敢恭维,所以就写了一个好用的类型判断,一般情况都够用的. function test(type) ...

  5. 看jquery3.3.1学js类型判断的技巧

    需要预习:call , typeof, js数据类型 1. isFunction中typeof的不靠谱 源码: var isFunction = function isFunction( obj ) ...

  6. JS类型判断typeof PK {}.toString.call(obj)

    参考链接:https://www.talkingcoder.com/article/6333557442705696719 先看typeof <!doctype html> <htm ...

  7. js类型判断:typeof与instanceof

    typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,function(函数),object(NULL,数组,对象),und ...

  8. js条件判断时隐式类型转换

    Javascript 中,数字 0 为假,非0 均为真 在条件判断运算 == 中的转换规则是这样的: 如果比较的两者中有布尔值(Boolean),会把 Boolean 先转换为对应的 Number,即 ...

  9. js类型判断

    console.log('---------------------'); var a="string"; console.log(a); //string var a=1; co ...

随机推荐

  1. excel数据导入到sqlserver中---------工作笔记

    调用页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys ...

  2. 在ASP.NET将程序中将上传的附件存储到另一台文件服务器上的实现

    假定有两台服务器:A和B,其中A为Web服务器(IP:192.123.1.1, 为iis发布程序的服务器 ),B为文件服务器(IP: 192.123.2.2) 在文件服务器B中某个磁盘下创建一个共享文 ...

  3. 使用Javascript限制文本框只允许输入数字

    很多时候需要用到限制文本框的数字输入,试过许多方法,都不太理想,遂决定自己实现一个来玩玩.曾经使用过的方法通过onkeydown事件来控制只允许数字: <input onkeydown=&quo ...

  4. dede当前位置各种写法

    方法一.Dedecms当前位置{dede:field name='position'/} 方法二.dede:field name='position' runphp='yes'}          $ ...

  5. 我的github今天大手笔分享,welcome——fork

    看项目名就知道是什么样子的demo,还包含了数据库文件,welcome fork!

  6. 简单实现图片间的切换动画 主要用到ViewPager

    简单实现图片间的切换动画 主要用到ViewPagerViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view.ViewPager类需要一个PagerAdapter适 ...

  7. List、Set、Map的使用

    1.List(接口) List接口的特点: a.List接口可以存放任意数据,且在接口中,数据可以重复. b.List中提供了高效的插入和移除多个元素的方法. List常用的子类 a.ArrayLis ...

  8. java获取对象属性类型、属性名称、属性值 【转】

    /** * 根据属性名获取属性值 * */ private Object getFieldValueByName(String fieldName, Object o) { try { String ...

  9. 【转】 iOS如何实现表格的折叠效果?

    原文 :  http://blog.csdn.net/youcanping2008/article/details/9202167 一.实现原理:就是在点击表格组头视图的时候,如果该表格视图的组展开了 ...

  10. Ext Sencha Cmd 6 环境安装

    Sencha Cmd安装笔记 在使用前端框架ExtJs前,需对其环境加以部署,对自己的安装过程加以记录. 建议大家在安装时,最好使用VPN,个中缘由,生在跳槽的程序袁应该都懂吧.不然ruby相关命令执 ...