源码中有这样一段:

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. eclipse导入maven项目, A resource exists with a different case: '/xxx'.

    eclipse 导入maven 项目出现 这是由于你的workspace里有相同的项目, 这时在metadata里可以看到所有的project信息 只需在eclipse的package explore ...

  2. P1184 高手之在一起(字典树模板题,hash算法, map)

    哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...

  3. 【转】bios与CMOS有什么区别

    正确的解释应该是: BIOS是软件.是程序! CMOS是芯片.是硬件! 实际上我们是通过BIOS这个程序,去设置CMOS里的参数的. CMOS是一块芯片,集成在主板上,里面保存着重要的开机参数,而保存 ...

  4. PHP删除数组中空值的方法介绍

    这篇文章主要介绍了PHP删除数组中空值的方法介绍,需要的朋友可以参考下 说来惭愧,以前在去掉数组的空值是都是强写foreach或者while的,利用这两个语法结构来删除数组中的空元素,简单代码如下: ...

  5. C# WebApi 获取客户端ip地址

    转自:http://www.cnblogs.com/weixing/p/5674078.html References required: HttpContextWrapper - System.We ...

  6. 【vue】vue +element 搭建项目,组件之间通信

    父子组件通信 父 通过props属性给 子传递数据 子 操作 父  this.$parent.XXX 子通过$emit传递参数 或者通过vue-bus vue-bus既可以实现父子组件之间的通信,也可 ...

  7. 003_python内置的@staticmethod详解

    python中的staticmethod 主要是方便将外部函数集成到类体中,美化代码结构,重点在不需要类实例化的情况下调用方法(类似java的静态方法) 如果你去掉staticmethod,在方法中加 ...

  8. Shell 文本处理三剑客之grep

    grep ♦参数 -E,--extended-regexp 模式是扩展正则表达式 -i,--ignore-case 忽略大小写 -n,--line-number 打印行号 -v,--invert-ma ...

  9. 简单的if多分支结构练习:用户录入 1-10的数字 , 1-7没奖品 , 8,9,10分别获得 3 2 1 等奖

    package com.summer.cn; import java.util.Scanner; /** * @author Summer *简单的if多分支结构练习 *用户录入 1-10的数字 , ...

  10. Shiro核心概述

    0.写在前面的话 最近在考虑权限相关的东西,于是就找到了Shiro,开涛老师的Shiro教程博客(<跟我学Shiro>)写得实在很好还带所有源码,所以我也就没有自己再总结各个阶段的笔记,只 ...