您的位置:首頁技術文章
文章詳情頁

javascript - 打印一個js對象的實際類型

瀏覽:114日期:2023-03-03 17:35:38

問題描述

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'

標簽: JavaScript
相關文章:
国产综合久久一区二区三区