您的位置:首页技术文章
文章详情页

javascript - 打印一个js对象的实际类型

【字号: 日期:2023-03-03 17:35:38浏览:40作者:猪猪

问题描述

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'

标签: JavaScript
相关文章: