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. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?2. docker api 开发的端口怎么获取?3. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?4. Docker for Mac 创建的dnsmasq容器连不上/不工作的问题5. golang - 用IDE看docker源码时的小问题6. 前端 - 类到底该如何去命名 .newsList 这种的命名难道真的不是过度语义化吗?~7. docker容器呢SSH为什么连不通呢?8. docker网络端口映射,没有方便点的操作方法么?9. docker不显示端口映射呢?10. docker gitlab 如何git clone?

网公网安备