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. 这是什么情况???2. javascript - 关于数组的循环遍历问题3. jupyter-notebook - Mac下启动jupyter notebook后没有Python的选项?4. 百度地图api - Android 百度地图点击线路图的问题?5. mysql sql where id in(25,12,87) 结果集如何用按照 25 12 87排序?6. javascript - 如何在外部点击,跳转到网页后,显示指定的模块。7. css3的transform问题8. 数据库创建失败9. javascript - 问下npm package json中的devDependencies 与dependencies的?10. mysql两种多表查询的区别?

网公网安备