javascript - 打印一个js对象的实际类型
问题描述
http.createServer((req,res)=>{ res.write(’hello world’); console.log(typeof res);// obj res.end();});
如何 查看req和res的具体对象类型,这样可以去文档中看具体详细api.typeof打印出的是object,我希望打印出的是http.ServerResponse
怎么搞
问题解答
回答1:打印函数的类信息:
function classof(obj){ if(typeof(obj)==='undefined')return 'undefined'; if(obj===null)return 'Null'; var res = Object.prototype.toString.call(obj).match(/^[objects(.*)]$/)[1]; if(res==='Object'){res = obj.constructor.name;if(typeof(res)!=’string’ || res.length==0){ if(obj instanceof jQuery)return 'jQuery';// jQuery build stranges Objects if(obj instanceof Array)return 'Array';// Array prototype is very sneaky return 'Object';} } return res;}// Exampleconsole.log(classof(new Date())); // => 'Date'
相关文章:
1. angular.js - 指令下的指令 面对上级指令ng-repeat的时候 ng-controller会出现多次的问题?2. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?3. golang - 用IDE看docker源码时的小问题4. dockerfile - [docker build image失败- npm install]5. docker-compose 为何找不到配置文件?6. java - servlet的init方法和选择Filter的init方法来加载配置文件,二者有何区别?7. dockerfile - 为什么docker容器启动不了?8. css - ul ol前边的标记如何调整样式呢9. android - 哪位大神知道java后台的api接口的对象传到前端后输入日期报错,是什么情况?求大神指点10. css - chrome浏览器input记录上次cookie信息后,有个黄色背景~如何去除!

网公网安备