源码中有这样一段:

class2type = {},
toString = class2type.toString,
 
function type(obj) {
//obj为null或者undefined时,直接返回'null'或'undefined'
  return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"
}
 
// Populate the class2type map
//填充class2type的值
$.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function (i, name) {
  class2type["[object " + name + "]"] = name.toLowerCase()
})

--------------------------------------------------------------------------------------------------------

在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。对于数组、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。

要想区别对象、数组单纯使用 typeof 是不行的。或者你会想到 instanceof 方法,例如下面这样:

  1.  
    var a = {};
  2.  
    var b = [];
  3.  
    var c = function () {};
  4.  
     
  5.  
    //a b c 都是 Object 的实例
  6.  
    console.log(a instanceof Object) //true
  7.  
    console.log(b instanceof Object) //true
  8.  
    console.log(c instanceof Object) //true
  9.  
     
  10.  
    //只有 Array 类型的 b 才是 Array 的实例
  11.  
    console.log(a instanceof Array) //false
  12.  
    console.log(b instanceof Array) //true
  13.  
    console.log(c instanceof Array) //false
  14.  
     
  15.  
    //只有 Function 类型的 c 才是 Function 的实例
  16.  
    console.log(a instanceof Function) //false
  17.  
    console.log(b instanceof Function) //false
  18.  
    console.log(c instanceof Function) //true

从以上代码来看,要判断复合数据类型,可以如下判断:

  1.  
    //对象
  2.  
    (a instanceof Object) && !(a instanceof Function) && !(a instanceof Function)
  3.  
    //数组
  4.  
    (a instanceof Object) && (a instanceof Array)
  5.  
    //函数
  6.  
    (a instanceof Object) && (a instanceof Function)

更简便的方式,即是使用 Object.prototype.toString.call() 来确定类型,ECMA 5.1 中关于该方法的描述[1]是这样的:

When the toString method is called, the following steps are taken:
If the this value is undefined, return “[object Undefined]”.
If the this value is null, return “[object Null]”.
Let O be the result of calling ToObject passing the this value as the argument.
Let class be the value of the [[Class]] internal property of O.
Return the String value that is the result of concatenating the three Strings “[object “, class, and “]”.

由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toString.call() 方法结果如下:

  1.  
    console.log(Object.prototype.toString.call(123)) //[object Number]
  2.  
    console.log(Object.prototype.toString.call('123')) //[object String]
  3.  
    console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
  4.  
    console.log(Object.prototype.toString.call(true)) //[object Boolean]
  5.  
    console.log(Object.prototype.toString.call({})) //[object Object]
  6.  
    console.log(Object.prototype.toString.call([])) //[object Array]
  7.  
    console.log(Object.prototype.toString.call(function(){})) //[object Function]

所有类型都会得到不同的字符串,几乎完美。

思考:使用return obj == null ? String(obj) : class2type[obj.toString()] || "object"   也是可以的,作者是先将Object的tostring赋值给toString,

用意应该是缓存变量, 便于压缩代码,
同时可减少在原型链中的查找次数(提高代码效率)

Object.prototype.toString.call()的更多相关文章

  1. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  2. instanceof, typeof, & Object.prototype.toString

    /** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...

  3. 判断一个变量的类型Object.prototype.toString.call

    var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';al ...

  4. Object.prototype.toString.call() 区分对象类型

    判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...

  5. Object.prototype.toString.call()进行类型判断

    为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...

  6. toStirng()与Object.prototype.toString.call()方法浅谈

    一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...

  7. JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈

    toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...

  8. JavaScript:Object.prototype.toString方法的原理

    在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...

  9. 【JavaScript】Object.prototype.toString.call()进行类型判断

    权声明:本文为博主原创文章,未经博主允许不得转载. op = Object.prototype, ostring = op.toString, ... function isFunction(it)  ...

  10. Object.prototype.toString.call() 区分对象类型(判断对象类型)

    在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种.对于数组. ...

随机推荐

  1. 这些Linux命令,让你的工作事半功倍!

    这些Linux命令,让你的工作事半功倍! 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较 ...

  2. 在Ubuntu18.04上使用Anaconda(python3.7)环境中安装tensorflow1.13.1

    由于清华镜像源迟迟没有更新tensorflow1.13.1导致python3.7不能使用tensorflow 这里有一个解决方法 管理员模式打开(一定要管理员模式 不然会导致权限不足) 输入 pip ...

  3. C# Lambda 表达式学习之(四):动态构建类似于 c => c.Age == 2 || c.Age == 5 || c => c.Age == 17 等等一个或多个 OrElse 的表达式

    可能你还感兴趣: 1. C# Lambda 表达式学习之(一):得到一个类的字段(Field)或属性(Property)名,强类型得到 2. C# Lambda 表达式学习之(二):LambdaExp ...

  4. 【ES6】函数

    函数默认值问题 在ES6之前,不能直接为函数指定默认值,但是ES6允许为函数的参数设置默认值 之前实现方式 function log(x, y) { y = y || 'World'; console ...

  5. 原生js 函数 callee属性

    一.在es5中,函数中有arguments参数,该参数是一个包含函数传入的参数的类数组. <script> function myArrgu(x){ console.log(argumen ...

  6. Linux进程管理 (1)进程的诞生

    专题:Linux进程管理专题 目录: Linux进程管理 (1)进程的诞生 Linux进程管理 (2)CFS调度器 Linux进程管理 (3)SMP负载均衡 Linux进程管理 (4)HMP调度器 L ...

  7. SQL开发规范

    一.SQL代码规范: 1.头部 --************************************************************************** --所属主题: ...

  8. 断路器(Curcuit Breaker)模式

    在分布式环境下,特别是微服务结构的分布式系统中, 一个软件系统调用另外一个远程系统是非常普遍的.这种远程调用的被调用方可能是另外一个进程,或者是跨网路的另外一台主机, 这种远程的调用和进程的内部调用最 ...

  9. Vue.js 系列教程 1:渲染,指令,事件

    原文:intro-to-vue-1-rendering-directives-events 译者:nzbin 如果要我用一句话描述使用 Vue 的经历,我可能会说“它如此合乎常理”或者“它提供给我需要 ...

  10. 如何向微软 Docs 和本地化社区提交翻译贡献

    Docs (docs.microsoft.com)是微软新版的文档网站,重新规划了各项技术栈的文档结构,看起来比 MSDN 可读性更好.虽然 Docs 提供了各种语言的版本,但大多是机器翻译,某些中文 ...