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. [python2]local variable referenced before assignment問(wèn)題2. Python2中code.co_kwonlyargcount的等效寫(xiě)法3. python中怎么對(duì)列表以區(qū)間進(jìn)行統(tǒng)計(jì)?4. javascript - 我的站點(diǎn)貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?5. mysql - 請(qǐng)問(wèn)數(shù)據(jù)庫(kù)字段為年月日,傳進(jìn)的參數(shù)為月,怎么查詢那個(gè)月所對(duì)應(yīng)的數(shù)據(jù)6. python - 如何判斷字符串為企業(yè)注冊(cè)名稱(chēng)7. javascript - vue+iview upload傳參失敗 跨域問(wèn)題后臺(tái)已經(jīng)解決 仍然報(bào)403,這是怎么回事啊?8. php - 微信開(kāi)發(fā)驗(yàn)證服務(wù)器有效性9. html - 移動(dòng)端radio無(wú)法選中10. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?
