Object.prototype.toString()方法返回一个代表该对象的字符串。

var o = new Object();
o.toString(); //"[object Object]"

使用toString()方法来检测对象类型

var toString = Object.prototype.toString;
toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math] // Since JavaScript ES5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]

封装为classof()函数

function classof(o){
if(o === null) return "Null";
if(o === undefined) return "Undefined";
//Object.prototype.toString()返回"[object Object]",然后call(o)参数,返回该类型,如number类型call(1),返回"[object Number]",再使用slice(截取类型部分)
return Object.prototype.toString.call(o).slice(8,-1);
}
console.log(classof(null)); //"Null"
console.log(classof(undefined)); //"Undefined"
console.log(classof(1)); //"Number"
console.log(classof("")); //"String"
console.log(classof(true)); //"Boolean"
console.log(classof({})); //"Object"
console.log(classof([])); //"Array"
console.log(classof(new Date())); //"Date"
console.log(classof(/./)); //"RegExp"
console.log(window); //window(这是客户端宿主对象)
console.log(classof(function f(){})); //定义一个自定义构造函数

Object.prototype.toString()的更多相关文章

  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. MySQL和Redis面试题小结

    MySQL专题 1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 ...

  2. 【DataStructure】Linked Data Structures

    Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. ...

  3. datanode启动失败

    当我动态加入一个hadoop从节点的之后,出现了一个问题: [root@hadoop current]# hadoop-daemon.sh start datanode starting datano ...

  4. CentOS6 配置FTP服务器

    编辑 删除 1.先检查有没有安装   rpm -q vsftpd 如果没有安装   yum install vsftpd 2.先关闭防火墙进行调试. service iptables stop 或者一 ...

  5. 高性能高并发网络库:StateThreads

    StateThreads是一个C的网络程序开发库,提供了编写高性能.高并发.高可读性的网络程序的开发库,轻量级网络应用框架 共也就3000行C代码 网络程序(Internet Application) ...

  6. js对数字的校验

    //-----------------------------------------------函数(1):允许输入正数和负数的表达式-------------------------------- ...

  7. Qt中的串口编程之一

    QtSerialPort 简介 功能介绍 SerialPort SerialPortInfo 源代码 编译和安装 配置编译环境 Perl只是在Qt5的时候才需要Qt4的情况下可以不配置 使用如下推荐步 ...

  8. 全局结果集,带参数的结果集和动态结果集(struts2)

    全局结果集: 当许多action都有共同的结果时,如果每个package都存在一个相同结果,会使得struts.xml比较臃肿,所以使用全局的结果集.一个包内的全局结果集可以通过包的继承而被其它包使用 ...

  9. MVC演化 (转至http://www.cnblogs.com/wubaiqing/archive/2011/10/26/2225438.html)

    MVC已经成为我们最常误用的模式,人们之所以常常误用MVC,很大程度上是因为混淆了不同的MVC变体. Classic MVC Classic MVC 大概上世纪七十年代,Xerox PARC的Tryg ...

  10. LabVIEW中数组的自动索引

    我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(A ...