源码中有这样一段:

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. RSA简介

    RSA概述 首先看这个加密算法的命名.很有意思,它其实是三个人的名字.早在1977年由麻省理工学院的三位数学家Rivest.Shamir 和 Adleman一起提出了这个加密算法,并且用他们三个人姓氏 ...

  2. UVA818-Cutting Chains(二进制枚举+dfs判环)

    Problem UVA818-Cutting Chains Accept:393  Submit:2087 Time Limit: 3000 mSec  Problem Description Wha ...

  3. UVA1354-Mobile Computing(二进制枚举子集)

    Problem UVA1354-Mobile Computing Accept:267  Submit:2232 Time Limit: 3000 mSec  Problem Description ...

  4. 小a的排列

    链接:https://ac.nowcoder.com/acm/contest/317/G来源:牛客网 小a有一个长度为nn的排列.定义一段区间是"萌"的,当且仅当把区间中各个数排序 ...

  5. 10 python 初学(Python 的编码解码)

    Python 2 : ASCII Python 3 :Unicode

  6. linux运行级别和开机流程

    linux有七个运行级别 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆 运行级别2:多用户状态(没有NF ...

  7. windows下使用svn命令行

    1.安装“Slik-Subversion-1.9.7-win32.zip”,将路径写入windows的path路径 2.在cmd下可以执行svn 3.应为tortoiseSVN的版本比1的版本低,导致 ...

  8. JSON.stringify()和JSON.parse()的作用

    (1)JSON.stringify() 从一个对象中解析出字符串 JSON.stringify({“a”:”1”,”b”:”2”}) 结果是:”{“a”:”1”,”b”:”2”}” (2)JSON.p ...

  9. 数组升序排序的方法Arrays.sort();的应用

    package com.Summer_0421.cn; import java.util.Arrays; /** * @author Summer * 数组升序排序的方法Arrays.sort();应 ...

  10. Luogu P1776 宝物筛选_NOI导刊2010提高(02)(多重背包模版)

    传送门 多重背包板子题, 多重背包就是每种东西有好几个,可以把它拆分成一个一个的01背包 优化:二进制拆分(拆成1+2+4+8+16+...) 比如18=1+2+4+8+3,可以证明18以内的任何数都 ...