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. html2canvas如何在元素隐藏的情况下生成截图

    html2canvas官网地址:http://html2canvas.hertzen.com/ github地址:https://github.com/niklasvh/html2canvas/ 从官 ...

  2. Flume1.5.0入门:安装、部署、及flume的案例

    转自:http://www.aboutyun.com/thread-8917-1-1.html 问题导读1.什么是flume2.flume的官方网站在哪里?3.flume有哪些术语?4.如何配置flu ...

  3. chrome 如何利用快捷键将光标移动到地址栏

    Windows: Ctrl + L 或 Alt + D 或 F6 Mac: Command + LLinux: Ctrl + L

  4. python读写word文档

    读: from docx import Document dir_docx = 'F:\Eclipse\workspace\Spider\cnblogs_doc\mytest - 副本.docx' d ...

  5. com.sun.awt.AWTUtilities.setWindowOpacity相关说明

    在eclipse中(jdk1.6.*)版本中出现编译不通过而报错,报错是因为这个包里面的方法不属于jdk正式版本,也就是不能保证下个版本还存在,所以编译器会拒绝,你可以在eclipse中如下设置: 选 ...

  6. 使用_snscanf_s转换十六进制时引起的内存越界

    //将Hex编码转换为指定编码格式的字符串 string Encoding::DecodeHexString(const string &strSrc, UINT code_page ) { ...

  7. html中可以自定义属性,,,妈的竟然才知道..

    html中可以自定义属性,,,妈的竟然才知道.. <input userinfo="没见过帅哥呀" />

  8. window设置TortoiseGit连接git不用每次输入用户名和密码

    1. 在Windows中添加一个HOME环境变量,值为%USERPROFILE%,如下图: 2. 在“开始>运行(快捷键:win+r)”中打开%Home%,然后在目录下新建一个名为“_netrc ...

  9. html5简单拖拽实现自动左右贴边+幸运大转盘

    此篇文章主要实现两个功能: 1.点击屏幕下方签到悬浮按钮: 2.弹出幸运大转盘,转盘抽奖签到 效果如图: ​ 在网上找了很多移动端拖拽的js实现方式,大部分都是这一种,html5的touch事件,但是 ...

  10. linux环境中查看主机型号(机器型号)

    需求说明: 今天一同事让统计测试环境主机型号,在此记录下. 操作过程: 1.通过dmidecode工具查询,产品型号(机器型号) [root@redhat6 ~]# dmidecode | grep ...