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. docker 17.03 怎么配置 registry mirror ?2. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????3. 前端 - @media query 使用出現的問題?4. html5 - datatables 加載不出來數據。5. 運行python程序時出現“應用程序發(fā)生異常”的內存錯誤?6. node.js - mongodb查找子對象的名稱為某個值的對象的方法7. 測試自動化html元素選擇器元素ID或DataAttribute [關閉]8. javascript - QQ第三方登錄的問題9. 利用IPMI遠程安裝centos報錯!10. spring-mvc - spring-session-redis HttpSessionListener失效

網公網安備