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. android有ldpi, mdpi, hdpi, xhdpi这些drawable文件夹,系统是依据什么去选择的?2. angular.js - angularjs 与requirejs集成3. android - textview在获取网络数据填充之后,占据的是默认的大小,点击之后才会包裹内容。4. Java 在内部类中访问变量。需要宣布为最终5. Java中的多人游戏。将客户端(玩家)连接到其他客户端创建的游戏6. html - 特殊样式按钮 点击按下去要有凹下和弹起的效果7. angular.js - ng-grid 和tabset一起用时,grid width默认特别小8. mysql中 when then 的优化9. html5 - 在一个页面中 初始了两个swiper 不知道哪里错了 一直不对10. html - CSS3能写出这种环状吗,不是环形进度条?
