源码中有这样一段:

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. 设计模式のDecoratorPattern(装饰器模式)----结构模式

    一.产生背景 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...

  2. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

  3. P2080 增进感情(背包DP)

    思路:将好感度x+y作为体积, 幸福度x-y作为作为价值, 然后就是一个经典的背包问题了.emmmmm,还可以特判一下,因为幸福度为0时就是最小了,没有必要看后面的了吧. 其实,我自己做的时候,沙雕的 ...

  4. @ModelAttribute

    在执行Controller方法前都会新建一个Map对象称为隐含模型,该Map对象是共享的,如果一个方法的入参为Map ModelAndMap ModelMap等类型,那么会把隐含模型当做入参赋给方法. ...

  5. conda install 出错

    在下载包时出现下面的错误: userdeMBP:pytorch user$ conda install -n deeplearning matplotlib Solving environment: ...

  6. DES对称加密算法详解和c++代码实现(带样例和详细的中间数据)

    特点: 1.DES是对称性加密算法,即加密和解密是对称的,用的是同一个密钥 2.DES只处理二进制数据,所以需要将明文转换成为2进制数据 3.DES每次处理64位的数据,所以应该将明文切割成64位的分 ...

  7. 关于vue的混入使用

    普通使用: 定义一个 mixin.js文件 随便定一些数据 记得后面导出 然后在需要用的文件 就可以获取了. 全局混合: 引入vue 全局注册混合 main.js 入口文件引入 然后就可以在所有页面使 ...

  8. 简单的自定义ViewGroup

    自定义ViewGroup需要重写onMeasure, onLayout等方法.下面是一个实例,4个View分别显示在四个角. public class MyGroup extends ViewGrou ...

  9. JS 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)

    时间戳转换为时间 // 时间戳转换为时间 function timestampToTime(timestamp, isMs = true) { const date = new Date(timest ...

  10. 实战Asp.Net Core:部署应用

    1.前言 某一刻,你已经把 .Net Core 的程序写好了.接下来,还可以做什么呢?那就是部署了. 作为一名开发工程师,如果不会部署自己开发的应用,那么这也是不完整的.接下来,我们就来说说,如何部署 ...