javascript - 打印一個(gè)js對(duì)象的實(shí)際類(lèi)型
問(wèn)題描述
http.createServer((req,res)=>{ res.write(’hello world’); console.log(typeof res);// obj res.end();});
如何 查看req和res的具體對(duì)象類(lèi)型,這樣可以去文檔中看具體詳細(xì)api.typeof打印出的是object,我希望打印出的是http.ServerResponse
怎么搞
問(wèn)題解答
回答1:打印函數(shù)的類(lèi)信息:
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. python - 《flask web 開(kāi)發(fā)》一書(shū),數(shù)據(jù)庫(kù)中多對(duì)多關(guān)系的實(shí)現(xiàn)問(wèn)題?2. 關(guān)于phpstudy設(shè)置主從數(shù)據(jù)庫(kù)3. mysql - 我用SQL語(yǔ)句 更新 行的時(shí)候,發(fā)現(xiàn)全部 中文都被清空了,請(qǐng)問(wèn)怎么解決?4. centos7 編譯安裝 Python 3.5.1 失敗5. python3.x - python3.5使用pyinstaller打包報(bào)錯(cuò)找不到libpython3.5mu.so.1.0等文件求解?6. phpStudy2017輕巧版mysql無(wú)法啟動(dòng)7. 為什么我輸入了refresh不會(huì)跳轉(zhuǎn)?請(qǐng)教大神支招!8. 為什么顯示數(shù)據(jù)丟失呢9. 為什么我寫(xiě)的PHP不行10. MySQL在什么情況下會(huì)被堵死?
