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 - 如何判断用户切换到了当前标签页?2. 关于java 泛型设计接口 导致的参数类型不匹配问题3. javascript - 在一些视频为主的h5页面中,是怎么做到获取视频缓冲进度或者说如何对video视频做预加载的4. datetime - Python如何获取当前时间5. 为什么要使用javascript函数包装器(添加在coffeescript中)“。call(this)”6. javascript - 为什么!function foo(){}返回false,!function foo(){}()返回true?7. javascript - 在nodejs的程序里面怎么理解IO呢,如何用一段代码来说明IO8. angular.js - angularjs 怎么封装 upload 上传9. javascript - 使用ionic建立start一个项目的时候,总是失败。10. MySQL启动错误

网公网安备