Object.prototype.toString()
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()的更多相关文章
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- instanceof, typeof, & Object.prototype.toString
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...
- 判断一个变量的类型Object.prototype.toString.call
var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';al ...
- Object.prototype.toString.call() 区分对象类型
判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...
- Object.prototype.toString.call()进行类型判断
为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...
- toStirng()与Object.prototype.toString.call()方法浅谈
一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...
- JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈
toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...
- JavaScript:Object.prototype.toString方法的原理
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...
- 【JavaScript】Object.prototype.toString.call()进行类型判断
权声明:本文为博主原创文章,未经博主允许不得转载. op = Object.prototype, ostring = op.toString, ... function isFunction(it) ...
- Object.prototype.toString.call() 区分对象类型(判断对象类型)
在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种.对于数组. ...
随机推荐
- CentOS6.5下安装Cloudstack
个人记录: 使用yum源安装,地址:http://mirrors.163.com/.help/CentOS6-Base-163.repo 后续待进行
- Select显示多级分类列表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- import_tasks: tasks/sometasks.yml
- import_tasks: tasks/sometasks.yml when: "'reticulating splines' in output" unarchive模块用 ...
- Laravel Debugbar
Installation Require this package with composer: composer require barryvdh/laravel-debugbar After up ...
- 我们要注意的Mysql基本安全设置
1.设置或修改Mysql root密码:默认安装后空密码,以mysqladmin命令设置密码: mysqladmin -uroot password "password" Mysq ...
- jquery easyui datagrid 分页实现
通常情况下页面数据的分页显示分成真假两种.真分页是依靠后台查询时控制调出数据的数量来实现分页,也就是说页面在后台对数据进行处理,仅传输当前需要页的数据到前台来显示.而假分页则是后台一次性将所有的数据一 ...
- *.ashx一般处理程序不能访问Session值的解决方法
<%@ WebHandler Language="C#" Class="productHandler" %> using System; using ...
- ElasticSearch使用代码
package elasticsearch01; import static org.junit.Assert.*; import java.util.HashMap; import java.uti ...
- JavaScript 杂乱的小总结
基本类型只有String.number.boolean.null.undefined,还有一个Object.存在装箱类型,不过后台自动转换. 通过new创建对象时,如果没有参数,可以省略“()”.-- ...
- 其它系统与domino系统单点登录的实现方式
其它系统与domino系统单点登录的实现方式 [背景] 随着企业中业务不断增多,用户处理不同的业务则须要频繁的切换不同的系统进行操作.而用户则须要记住各个系统的username.password ...