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 - 关于jsp中嵌入js代码的问题2. javascript - 百度坐标,返回目标附近有哪些坐标3. android-webview - Android 7.0 webview不自动刷新4. javascript - 网页打印页另存为pdf的代码一个问题5. javascript - 一个JS多元运算符的问题6. javascript - [MUI 子webview定位]7. javascript - 判断用户网络环境8. javascript - 有一个异步获取数据的函数A,其他依赖这个A得到的数据的函数是否都必须是异步的?9. javascript - 为什么var obj = {}创建对象的方法里面不能用this.xxx来声明属性 ?10. angular.js - angurlarJS的安卓的微信端中用加载ngCookies会出错的问题

网公网安备