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. 將SQLServer數據同步到MySQL 用什么方法?2. 為什么我ping不通我的docker容器呢???3. android - webview 自定義加載進度條4. docker安裝后出現Cannot connect to the Docker daemon.5. numpy - python [:,2][:,None]是什么意思6. javascript - 微信小程序封裝定位問題(封裝異步并可能多次請求)7. 并發模型 - python將進程池放在裝飾器里為什么不生效也沒報錯8. javascript - 微信小程序限制加載個數9. javascript - 微信音樂分享10. python 怎樣用pickle保存類的實例?
