js的深入学习课程Object.prototype.toString.call()
1、通过 Object.prototype.toString.call() 进行类型判断
function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call('') // "[object String]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(Math) // "[object Math]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call([]) // "[object Array]"
利用这个特性,可以写出一个比typeof
运算符更准确的类型判断函数。
var type = function (o){
var s = Object.prototype.toString.call(o);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
}; type({}); // "object"
type([]); // "array"
type(5); // "number"
type(null); // "null"
type(); // "undefined"
type(/abcd/); // "regex"
type(new Date()); // "date"
在上面这个type
函数的基础上,还可以加上专门判断某种类型数据的方法。
['Null',
'Undefined',
'Object',
'Array',
'String',
'Number',
'Boolean',
'Function',
'RegExp',
'NaN',
'Infinite'
].forEach(function (t) {
type['is' + t] = function (o) {
return type(o) === t.toLowerCase();
};
});
type.isObject({}) // true
type.isNumber(NaN) // true
type.isRegExp(/abc/) // true
var
oP = Object.prototype,
toString = oP.toString;
function
typeOf(value) {
if
(
null
=== value) {
return
'null'
;
}
var
type =
typeof
value;
if
(
'undefined'
=== type ||
'string'
=== type) {
return
type;
}
var
typeString = toString.call(value);
switch
(typeString) {
case
'[object Array]'
:
return
'array'
;
case
'[object Date]'
:
return
'date'
;
case
'[object Boolean]'
:
return
'boolean'
;
case
'[object Number]'
:
return
'number'
;
case
'[object Function]'
:
return
'function'
;
case
'[object RegExp]'
:
return
'regexp'
;
case
'[object Object]'
:
if
(undefined !== value.nodeType) {
if
(3 == value.nodeType) {
return
(/\S/).test(value.nodeValue) ?
'textnode'
:
'whitespace'
;
}
else
{
return
'element'
;
}
}
else
{
return
'object'
;
}
default
:
return
'unknow'
;
}
}
js的深入学习课程Object.prototype.toString.call()的更多相关文章
- 使用Object.prototype.toString.call()方法精确判断对象的类型
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object. 对于null.array.function. ...
- 浅谈Object.prototype.toString.call()方法
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object.对于null.array.function.o ...
- js 中调用 Object.prototype.toString()来检测对象的类型
1.使用toString()方法来检测对象类型 可以通过toString() 来获取每个对象的类型.为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Fun ...
- JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()
1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...
- js中[object Object]与object.prototype.toString.call()
最近在用node读取文件中的json数据后,用JSON.parse()转成了json,然后响应数据传给前端,发现输出值object对象时显示[object object],在这里我们来看一下他的具体意 ...
- js中通过Object.prototype.toString方法----精确判断对象的类型
判断是否为函数 function isFunction(it) { return Object.prototype.toString.call(it) === '[object Func ...
- js Object.prototype.toString.call()
Object.prototype.toString.call(obj)使用方法以及原理 这几天看vue-router的源码 发现了Object.prototype.toString.call()这 ...
- js精确判断数据类型为何用Object.prototype.toString.call()而不是Object.prototype.toString()
有何区别,为何一定要通过call. 我们知道call是用来改变函数作用域的,Object.prototype.toString.call在这儿也是用来改变作用域的. Object.prototype. ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
随机推荐
- Apache+PHP配置PATHINFO的一个小问题
使用ThinkPHP示例,设置'URL_MODEL' => 2,发现提示: No input file specified 应该是PATHINFO配置的问题,试 ...
- at com.mysql.jdbc.SQLError.createSQLException
WARN run, com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1de6191 -- APPARENT DEA ...
- 最新CoreData封装Demo
1.创建CoreData文件 2. 3. 4. UploadCoreDataAPI.h // // UploadCoreDataAPI.h // dsafsda // // Created by 冯敏 ...
- MySQL的keepalived高可用监控脚本
MySQL的keepalived高可用监控脚本 MySQL(或者其它服务)的keepalived高可用监控脚本 开发脚本需求 :我们知道,keepalive是基于虚拟ip的存活来判断是否抢占maste ...
- 基于Amoba实现mysql主从读写分离
一.Amoeba简介 Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特 ...
- MongoDB学习笔记(4)--collection
MongoDB 创建集合 本章节我们为大家介绍如何使用 MongoDB 来创建集合. MongoDB 中使用 createCollection() 方法来创建集合. 语法格式: db.createCo ...
- SteveY对Amazon和Google平台的吐槽
Steve Yegge, Amazon的前员工,现任Google员工,其本来想在Google+上和Google的员工讨论一些关于平台的东西,结果不小心把圈子设成了Public,结果这篇文章就公开给了全 ...
- linux下配置tomcat集群的负载均衡
linux下配置tomcat集群的负载均衡 一.首先了解下与集群相关的几个概念集群:集群是一组协同工作的服务实体,用以提供比单一服务实体更具扩展性与可用性的服务平台.在客户端看来,一个集群就象是一个服 ...
- EndNote 输出样式模板(根据国家标准制订)
EndNote 输出样式模板(根据国家标准制定) EndNote 相当于一个数据库,将添加/导入的文献存档.需要引用文献的时候就从中选择一个插入到文档中,EndNote 会自动给你编号.在文档末尾 ...
- 解决Myeclipse中导入自定义的配色方案后,JSP中的js代码块为白色背景的问题
捣鼓了大半个上午,终于搞定.这样设置就可以了: 点击MyEclipse上方的菜单栏中的window菜单.选择Preferences菜单项.在弹出的窗口的左侧树形菜单依次选择:MyEclipse.Fil ...