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. 在应用配置文件 app.php 中找不到’route_check_cache’配置项2. html按键开关如何提交我想需要的值到数据库3. HTML 5输入框只能输入汉字、字母、数字、标点符号?正则如何写?4. gvim - 谁有vim里CSS的Indent文件, 能缩进@media里面的5. 跟着课件一模一样的操作使用tp6,出现了错误6. PHP类属性声明?7. javascript - 求助canvas绘制马赛克的问题,老是取色不准8. java - 安卓接入微信登录,onCreate不会执行9. javascript - 请教如何获取百度贴吧新增的两个加密参数10. html - 微信浏览器h5<video>标签问题
