为什么类型判断用到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. linux 设置mysql 数据库编码utf8

    转载地址http://www.linuxidc.com/Linux/2015-08/121676.htm在MySQL数据库中, 当插入数据无法支持中文时, 可将该数据库的编码集设置为utf8, 故在创 ...

  2. hibernate----(Hql)另一种查询---利用Criteria类

    package com.etc.test; import java.util.List; import org.hibernate.Criteria;import org.hibernate.Sess ...

  3. js022-高级技巧

    js022-高级技巧 本章内容: 使用高级函数 防篡改对象 Yielding Timers 22.1 高级函数 1.安全的类型检测 2.作用域安全的构造函数 构造函数实际上是一个使用new操作符调用的 ...

  4. 基础SQL语句

    SQL语句: 1.插入 方法一: "INSERT INTO [DB].[dbo].[T_Table] ([ID],[Name],[Amount],[Creater],[CreatedOn], ...

  5. ubuntu rhythmbox乱码解决方法

    因为安装的是双系统,所以音乐文件在win的盘下面,所以采用的方法是 1. 首先,需要有软件包mid3iconv.如果你的系统中没有安装它,可以通过如下代码自动安装:sudo apt-get insta ...

  6. eclipse里怎么用命令行输入args

    eclipse中给java应用传args参数的方法如下:1.先写好Java代码,比如文件名为IntArrqy.java:2.在工具栏或菜单上点run as下边有个Run Configuration:3 ...

  7. HTML学习笔记——块级标签、行级标签、图片标签

    1>块级标签.行级标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  8. 给IOS系统的微信页面赋Title

    给页面赋一个title是最平常不过的事情了,不过在IOS下动态给页面赋title可不是平常的事情. 看代码: function setIOStitle(title) { $body = $('body ...

  9. SSAO相关资料

    http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-simple-and-pract ...

  10. Robot Framework--05 案例设计之流程与数据分离

    转自:http://blog.csdn.net/tulituqi/article/details/7651049 这一讲主要说一下案例设计了.还记得我们前面做的case么?先打开浏览器访问百度,输入关 ...