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. javascript - 百度地图自己的产品可以精确定位,为什么我调用他的公开API却必须经过用户同意才能获取精确定位,而且还慢?2. python - 一个域名可以绑定多台服务器吗?3. Windows环境开俩个CMD窗口操作MySQL测试加锁的疑问4. 怎么学好php5. 后台系统的左侧导航窗格如何修改6. javascript - echart+百度地图7. mybatis - Java关于Mysql的随机id生成8. mysql - 数据库建字段,默认值空和empty string有什么区别 1109. 网页爬虫 - Python爬虫入门知识10. python - import里的dot是以哪个路径作为根目录的?