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. boot2docker无法启动2. docker-compose中volumes的问题3. docker容器呢SSH为什么连不通呢?4. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””5. java - SSH框架中写分页时service层中不能注入分页类6. docker安装后出现Cannot connect to the Docker daemon.7. node.js - antdesign怎么集合react-redux对input控件进行初始化赋值8. 关于docker下的nginx压力测试9. nignx - docker内nginx 80端口被占用10. 老师,按tab键不起作用怎么回事

网公网安备