javascript - 打印一個js對象的實際類型
問題描述
http.createServer((req,res)=>{ res.write(’hello world’); console.log(typeof res);// obj res.end();});
如何 查看req和res的具體對象類型,這樣可以去文檔中看具體詳細(xì)api.typeof打印出的是object,我希望打印出的是http.ServerResponse
怎么搞
問題解答
回答1:打印函數(shù)的類信息:
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'
相關(guān)文章:
1. docker容器呢SSH為什么連不通呢?2. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””3. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?4. nignx - docker內(nèi)nginx 80端口被占用5. debian - docker依賴的aufs-tools源碼哪里可以找到啊?6. 前端 - ng-view不能加載進(jìn)模板7. android clickablespan獲取選中內(nèi)容8. angular.js - angular內(nèi)容過長展開收起效果9. python - from ..xxxx import xxxx到底是什么意思呢?10. angular.js - ng-grid 和tabset一起用時,grid width默認(rèn)特別小
