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. pku1204 Word Puzzles AC自动机 二维字符串矩阵8个方向找模式串的起点坐标以及方向 挺好的!

    /** 题目:pku1204 Word Puzzles 链接:http://poj.org/problem?id=1204 题意:给定一个L C(C <= 1000, L <= 1000) ...

  2. javaScript之function定义

    背景知识 函数定义 在javaScript中,function的定义有3种: 1.匿名定义                function(){} 2.非匿名定义                fun ...

  3. Web下的整体测试 --性能测试及优化思路

    随着Internet的日益普及,现在基于B/S结构的大型应用越来越多,可如何对这些应用进行测试成为日益迫切的问题.有许多测试人员来信问我B/S的测试如何做,由于工作较繁忙,对大家提出的问题也是头痛医头 ...

  4. 启动hadoop 2.6遇到的datanode启动不了

    转自 http://blog.csdn.net/zhangt85/article/details/42078347 查看日志如下: 2014-12-22 12:08:27,264 INFO org.m ...

  5. 【转】@JoinColumn 详解

    在address中没有特殊的注解. 在Person中对应到数据库里面就有一个指向Address的外键. 我们也可以增加注释指定外键的列的名字,如下:@OneToOne(cascade=CascadeT ...

  6. imx6 fec分析

    /***************************************************************************** * imx6 fec分析 * 本文主要分析 ...

  7. jquery widgets 弹框

    <div id='dialog' style="display:none;"> <div style="text-align:center;" ...

  8. 【R markdown】rmysql乱码问题

    统计数据遇到在Rmarkdown文档中通过rmysql查询中文结果乱码的现象. 数据库编码 utf8 rmd编码utf8 猜测是生成的html编码非utf8 解决方案是: dbSendQuery(co ...

  9. DOTween中的Time.Scale

    因为在做游戏暂停的时候通常会使用Time.Scale = 0 ,可是暂停的时候UI如果需要继续有动画怎么办呢?在DoTween中只需要设置         tweener.SetUpdate(true ...

  10. sftp,get命令使用*通配符的方式获取批量的文件

    需求描述: 今天在使用sftp进行get文件的时候,有很多文件名类似的文件,以为还是需要一个一个get 后来发现get也可以使用通配符的方式进行匹配获取多个文件,在此记录下 操作过程: 1.通过sft ...