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 images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?2. python的前景到底有大?如果不考慮數據挖掘,機器學習這塊?3. Matlab和Python編程相似嗎,有兩種都學過的人可以說說嗎4. javascript - Html5做移到端定位,獲取授權拒絕了怎么辦?5. javascript - 關于audio標簽暫停的問題6. 大家好,請問在python腳本中怎么用virtualenv激活指定的環境?7. 網頁爬蟲 - 用Python3的requests庫模擬登陸Bilibili總是提示驗證碼錯誤怎么辦?8. javascript - 微信小程序封裝定位問題(封裝異步并可能多次請求)9. javascript - Web微信聊天輸入框解決方案10. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應
