为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢?

然后翻了一下资料:

Typeof

在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "object"。 

Object.prototype.toString.call():

ECMA 对Object.prototype.toString的解释
  1. Object.prototype.toString ( )
  2. When the toString method is called, the following steps are taken:
  3. If the this value is undefined, return "[object Undefined]".
  4. If the this value is null, return "[object Null]".
  5. Let O be the result of calling ToObject passing the this value as the argument.
  6. Let class be the value of the [[Class]] internal property of O.
  7. Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

然后举了个例子:

var oP = Object.prototype,

toString = oP.toString;  
  
console.log(toString.call([]));//[object Array]  
console.log(toString.call(''));//[object String]  
console.log(toString.call({a: ''}));//[object Object]  
console.log(toString.call(//));//[object RegExp]  
console.log(toString.call());//[object Number]  
console.log(toString.call(undefined));//[object Undefined]  
console.log(toString.call(null));//[object Null]  

Object.prototype.toString.call()进行类型判断的更多相关文章

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

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

  2. 使用Object.prototype.toString.call()方法精确判断对象的类型

    在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object. 对于null.array.function. ...

  3. js的深入学习课程Object.prototype.toString.call()

    1.通过 Object.prototype.toString.call() 进行类型判断 function isArray(obj) { return Object.prototype.toStrin ...

  4. Object.prototype.toString.call() 、 instanceof 以及 Array.isArray()判断数组的方法的优缺点

    1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...

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

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

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

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

  7. Object.prototype.toString.call()方法浅谈

    使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Obj ...

  8. Object.prototype.toString的应用

    使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value)1.判断基本类型: Obje ...

  9. 前端面试题1:Object.prototype.toString.call() 、instanceof 以及 Array.isArray()三种方法判别数组的优劣和区别

    1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...

随机推荐

  1. HDU1392Surround the Trees(凸包判断 + 求周长)

    http://www.cnblogs.com/hmhard/archive/2013/02/05/2893035.html 这是判断三角区域那块写的不好. 判断凸包的方法: 1.将所有点按照y从小到大 ...

  2. Bitmap四种属性

    http://blog.csdn.net/rabbit_in_android/article/details/49967461 Bitmap: (1)     public Bitmap (int w ...

  3. swfUpload 上传图片

    前端: <script src="~/Scripts/swfupload/swfupload.js"></script> <script src=&q ...

  4. nginx https性能优化

    影响HTTPS速度的主要原因:秘钥交换算法 常见的密钥交换算法有 RSA,ECDHE,DH,DHE 等算法.它们的特性如下: RSA:算法实现简单,诞生于 1977 年,历史悠久,经过了长时间的破解测 ...

  5. spring-boot-note

    1 java配置和注解配置相结合,不需要任何的xml配置即可 2 spring tool suite 3 src/main/resources/banner.txt http://patorjk.co ...

  6. win7搭建web服务器

    首先找到安装的tomcat软件,打开里面的webapp文件夹,在里面新建一个文件夹用作web应用程序访问端地址,然后再到新建的文件夹test里放入想要被访问的东西,这里我用的是一个测试的页面test. ...

  7. k-nearest-neighbor算法

    1. kNN 1.1 基本的kNN模型 kNN(k-nearest neighbor)的思想简单来说就是,要评价一个未知的东西U,只需找k个与U相似的已知的东西,并通过k个已知的,对U进行评价.假如要 ...

  8. 删除ibus之后导致系统设置进不了

    sudo apt-get instal ubuntu-desktop 快捷键调出sogou拼音,默认为"ctrl+,"

  9. HTML学习笔记——CSS初涉

    1>嵌入式css写法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  10. 快捷键_Mac

    苹果Mac系统常用快捷键 Command+Tab 任意情况下切换应用程序 - 向前循环 Shift+Command+Tab 切换应用程序 - 向后循环 Command+L 当前程序是浏览器时,可以直接 ...