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. java - MySQL中,使用聚合函數+for update會鎖表嗎?2. mysql優化 - 關于mysql分區3. vue.js - vue 打包后 nginx 服務端API請求跨域問題無法解決。4. node.js - 在vuejs-templates/webpack中dev-server.js里為什么要exports readyPromise?5. objective-c - iOS開發支付寶和微信支付完成為什么跳轉到了之前開發的一個app?6. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應7. javascript - ionic2 input autofocus 電腦成功,iOS手機鍵盤不彈出8. html5 - 如何實現帶陰影的不規則容器?9. javascript - 為什么這個點擊事件需要點擊兩次才有效果10. java - Atom中文問題
