javascript - 打印一個(gè)js對(duì)象的實(shí)際類型
問(wèn)題描述
http.createServer((req,res)=>{ res.write(’hello world’); console.log(typeof res);// obj res.end();});
如何 查看req和res的具體對(duì)象類型,這樣可以去文檔中看具體詳細(xì)api.typeof打印出的是object,我希望打印出的是http.ServerResponse
怎么搞
問(wèn)題解答
回答1:打印函數(shù)的類信息:
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. docker綁定了nginx端口 外部訪問(wèn)不到2. docker網(wǎng)絡(luò)端口映射,沒(méi)有方便點(diǎn)的操作方法么?3. docker容器呢SSH為什么連不通呢?4. thinkphp5.1學(xué)習(xí)時(shí)遇到session問(wèn)題5. nignx - docker內(nèi)nginx 80端口被占用6. angular.js - angular內(nèi)容過(guò)長(zhǎng)展開(kāi)收起效果7. macos - mac下docker如何設(shè)置代理8. php - 第三方支付平臺(tái)在很短時(shí)間內(nèi)多次異步通知,訂單多次確認(rèn)收款9. docker images顯示的鏡像過(guò)多,狗眼被亮瞎了,怎么辦?10. 前端 - ng-view不能加載進(jìn)模板
